欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用JavaScript獲取地址欄參數(shù)的方法

 更新時(shí)間:2014年12月19日 15:42:27   投稿:hebedich  
這篇文章主要介紹了幾種使用JavaScript獲取地址欄參數(shù)的方法以及注意事項(xiàng),非常的實(shí)用,這里推薦給大家

復(fù)制代碼 代碼如下:

/**
 *  獲取地址欄參數(shù)
 *
 * @example GetUrlString('id')
 *
 * @desc    調(diào)用時(shí)加上判斷,保證程序不會(huì)出錯(cuò)
 *             var myurl = GetUrlString('id');
 *             if (myurl != null && myurl.toString().length > 1) {
 *                  alert(GetUrlString("id")); 
 *             }
 *
 * @param String  param 要獲取地址欄中的參數(shù)名
 * @return String Value
 * @type String
 *
 * @name GetUrlString()
 *
 */
function GetUrlString(param) {
    var sValue = location.search.match(new RegExp("[\?\&]" + m + "=([^\&]*)(\&?)", "i"));
    return sValue ? decodeURI(sValue[1]) : decodeURI(sValue);
}

調(diào)用的時(shí)候統(tǒng)一做這樣的判斷,避免你沒有傳參數(shù)的話,比如你的地址是 abc.html 后面沒有參數(shù),那強(qiáng)行輸出調(diào)用結(jié)果有的時(shí)候會(huì)報(bào)錯(cuò)

復(fù)制代碼 代碼如下:

window.onload = function() {   
    var myurl = GetParm("id");
    if (myurl != null && myurl.toString().length > 1) {
        alert(GetParm("id"));
    }
}

這樣就不會(huì)報(bào)錯(cuò)了!

注釋:ECMAScript v3 已從標(biāo)準(zhǔn)中刪除了 unescape() 函數(shù),并反對(duì)使用它,因此應(yīng)該用 decodeURI() 和 decodeURIComponent() 取而代之。

小伙伴們是否理解了如何使用JavaScript獲取地址欄參數(shù)了呢,有疑問(wèn)的話就留言吧。

相關(guān)文章

最新評(píng)論