js中怎么获取url后面的多个参数
程序员文章站
2024-02-19 08:18:28
...
例如 :url路径如下
url: http://localhost:8080/Demo/Demo.jsp?userid=001
js代码获取url中"?"符后的字串(参数):
var url = document.location.href; // 获取url完整路径
var theRequest = {};
if (url.indexOf("?") != -1) {
var str = url.substr(url.indexOf("?")+1); // str = "userid=001"
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
var st = strs[i].split("="); //str = (2) ["userid","001"]
if(st.length > 1)
theRequest[st[0]] = st[1]; // theRequest = {userid : "001"}
}
}
this.user = theRequest.userid; // 此时 this.user的值为001
```