- 相關(guān)推薦
JavaScript如何獲取地址欄中傳遞參數(shù)
導(dǎo)語:JavaScript如何獲取地址欄中傳遞參數(shù),通過下面的閱讀你會(huì)有所收獲,跟著小編一起來了解一下吧。
第一種:正則匹配法
這種方法其實(shí)原理和上一種方法類似,都是從URL中提取,只是提取的方法不同而已。
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
第二種:字符串拆分法
window.location.href 或者 location.href 或者 window.location 獲得地址欄中的所有內(nèi)容
decodeURI()可以解碼地址欄中的數(shù)據(jù) 恢復(fù)中文數(shù)據(jù)
window.search 獲得地址欄中問號(hào)及問號(hào)之后的數(shù)據(jù)
//獲取地址欄里(URL)傳遞的參數(shù)
function GetRequest(value) {
//url例子:www.bicycle.com?id="123456"&Name="bicycle";
var url = decodeURI(location.search); //?id="123456"&Name="bicycle";
var object = {};
if(url.indexOf("?") != -1)//url中存在問號(hào),也就說有參數(shù)。
{
var str = url.substr(1); //得到?后面的字符串
var strs = str.split("&"); //將得到的參數(shù)分隔成數(shù)組[id="123456",Name="bicycle"];
for(var i = 0; i < strs.length; i ++)
{
object[strs[i].split("=")[0]]=strs[i].split("=")[1]
}
}
return object[value];
}
【JavaScript如何獲取地址欄中傳遞參數(shù)】相關(guān)文章:
網(wǎng)頁開發(fā)中JavaScript傳遞參數(shù)方法比較08-02
java語言參數(shù)傳遞介紹06-12
如何檢查JavaScript變量的類型09-19
JavaScript如何實(shí)現(xiàn)JSON.stringify09-19
電腦如何自動(dòng)獲取ip地址06-02
網(wǎng)絡(luò)運(yùn)營:如何獲取、分析數(shù)據(jù)09-14
Java編程中獲取路徑的方法09-06