方法1:通过正则表达式(推荐)
1.1定义获取指定参数值得函数,name为url的参数名
function
getQueryString(name) {
let reg =
new
RegExp(
"(^|&)"
+ name +
"=([^&]*)(&|$)"
,
"i"
);
let r = window.location.search.substr(1).match(reg);
if
(r !=
null
) {
return
unescape(r[2]);
};
return
null
;
}
1.2调用函数获取参数值
let 参数1 = GetQueryString("参数名1"));
方法2:split拆分法
2.1定义拆分函数,参数为要查询的参数名
function
getQueryVariable(variable){
let query = window.location.search.substring(1);
let vars = query.split(
"&"
);
for
(let i=0;i<vars.length;i++) {
let pair = vars[i].split(
"="
);
if
(pair[0] == variable){
return
pair[1];}
}
return
(
false
);
}
2.2调用函数,返回参数结果,如果没有该参数则返回false
let 参数1 = getQueryVariable("参数名1");
标签:return,URL,JS,参数,split,pair,let,参数值
From: https://www.cnblogs.com/ynnotes/p/17637597.html