有時候我們在網頁上需要獲取url中某個參數的值,如http:www.test.com/index.html?value1=1&value2=2;
這時需要獲取value1或者value2的值,可以採用以下的方法進行獲取:
function getUrlParamvalue(paramName) {
var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if(r != null) {
return decodeURIComponent(r[2]);
}
return null;
}
其中 window.location.search獲取的url中含有?及其之後的字符串。以http:www.test.com/index.html?value1=1&value2=2爲例,window.location.search獲取的字符串就是?value1=1&value2=2
獲取參數,舉個栗子(獲取value2的值):
以上。