获取url中“?”之后参数
function GetUrlParam(name) {
// 获取params
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) {
// return unescape(r[2]); // 中文乱码
return decodeURI(r[2]);
} else {
return null;
}
}
- window.location.search 是获得url中"?"之后的字符串。
- match() 是将字符串按照正则获取匹配值。
- decodeURI()对 encodeURI() 函数编码过的 URI 进行解码。
示例
当网址页面请求到 www.abc.com/view/test.html?id=123
在 test.html 内js中调用GetUrlParam(“id”),则获取到参数123。