js 获取url参数

 1 //  示例urlStr:http://html/index.html?user=admin&pwd=123456
 2 
 3 function getRequest(urlStr) {
 4     if (typeof urlStr == "undefined") {
 5         var url = decodeURI(location.search); //获取url中"?"符后的字符串
 6     } else {
 7         var url = "?" + urlStr.split("?")[1];
 8     }
 9     var obj = new Object();
10     if (url.indexOf("?") != -1) {
11         var str = url.substr(1);
12         strs = str.split("&");
13         for (var i = 0; i < strs.length; i++) {
14             obj[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
15         }
16     }
17     return obj;
18 }
19 
20 var params = getRequest();
21 console.log(params); //{"user":"admin","pwd":"123456"}

 

posted @ 2020-07-14 17:27  PromiseOne  阅读(199)  评论(0编辑  收藏  举报