- 方法代码参考
// @Desc: 获取URL路径上查询参数值
// @params: urlStr:完整URL路径字符串,name:查询参数名
// @return: URL查询参数值
function getUrlParamVal(urlStr, name){
var url = new URL(urlStr);
var query = url.search.substr(1);
var vars = query.split("&");
for (var i=0; i<vars.length; i++) {
var pair = vars[i].split("=");
if(pair[0] == name){return pair[1];}
}
return null;
}
- 使用实例
var urlString = "https://www.example.com:8000/path?userNo=000123";
console.log(getUrlParamVal(urlString, "userNo")); //输出 000123