javascript添加url querystring
1 //获取url中的参数 2 getQueryString(pname) { 3 let reg = new RegExp("(^|&)" + pname + "=([^&]*)(&|$)", 'i'); // 匹配目标参数 4 let result = window.location.search.substr(1).match(reg); // 对querystring匹配目标参数 5 if (result != null) { 6 return decodeURIComponent(result[2]); 7 } else { 8 return null; 9 } 10 }, 11 //获取url中的参数 12 setQueryString(pname, pval) { 13 let vurl = window.location.pathname + window.location.search; 14 if(this.getQueryString(pname) != null) 15 { 16 let reg = new RegExp("([?&])" + pname + "=[^&]+", 'i'); 17 vurl = vurl.replace(reg, (...pargs)=>{ 18 return `${pargs[1]}${pname}=${pval}`; 19 }); 20 } 21 else 22 { 23 let vsymbol = '&'; 24 if(!window.location.search.length) 25 vsymbol = '?'; 26 vurl += `${vsymbol}${pname}=${pval}`; 27 } 28 window.history.replaceState({}, '', vurl); 29 },