js获取url参数
程序员文章站
2024-02-17 22:15:52
...
- 使用方式
console.log(GetRequest(‘lng’))
输出效果:
url: localhost:8001?lng=123123
输出:
123123
function GetRequest(param) {
let url = location.href; //获取url中"?"符后的字串
let params = url.match(/\?.*/);
if (!params) return;
url = params[0];
const theRequest= {};
if (url.indexOf("?") != -1) {
const str = url.substr(1);
const strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest[param];
}