从浏览器的url中获取查询字符串的参数

时间:2022-06-13 15:33:10

正则表达式:

 function getQuery(name){
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r != null)
return decodeURI(r[2]);
return null;
}

截取:

 function fn() {
var url = decodeURI(window.location.href);
var args = url.split('?');
if (args[0] === url)
return false;
var arr = args[1].split('&');
var obj = {};
for (var i = 0; i < arr.length; i++) {
var arg = arr[i].split('=');
obj[arg[0]] = arg[1];
}
return obj;
}