需求是html页面动态展示参数
url示例:http://localhost:8080/xiexinxx0225/article/details?code=230225
实现:
//根据QueryString参数名称获取值
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r != null) {
return decodeURIComponent(r[2]);
}
return '';
}
var value= getQueryString("code");
alert(value);
说明:
其中"(^|&)" + name + "=([^&]*)(&|$)"的意思就是取以name开头或者以&开头以&或者以$结尾的字符串
window.location.search.substr(1).match(reg)的意思是找出?之后匹配reg的字符串
decodeURIComponent(r[2])的意思是对所选的字符串进行解码