js 获取 url 中的参数
方法一:
function getParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
//例如网址是 https://www.baidu.com?name="blue"
console.log(getParam("name")); //blue
方法二:
function getParam(){
var url = location.search;
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
// 例如网址是: https://www.baidu.com?name="blue"
var param = getParam();
console.log(param.name); // blue