方法一:采取正则表达式获取地点栏参数 (代码轻巧,重点正则)
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; }调用方法:
let 参数1 = GetQueryString("参数名1"));
方法二:split拆分法 (代码较复杂,较易明白)
function GetRequest() { const url = location.search; //获取url中"?"符后的字串 let theRequest = new Object(); if (url.indexOf("?") != -1) { let str = url.substr(1); strs = str.split("&"); for(let i = 0; i < strs.length; i ++) { theRequest[strs.split("=")[0]]=unescape(strs.split("=")[1]); } } return theRequest;}调用方法:
let Request = new Object();
Request = GetRequest();
var 参数1,参数2 ...;
参数1 = Request['参数1'];
参数2 = Request['参数2'];
参数... = Request['参数...'];
方法三:split拆分法(易于明白,代码中规)
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.split("="); if(pair[0] == variable){return pair[1];} } return(false);}调用方法:
let 参数1 = getQueryVariable("参数名1"); 增补URL知识: