获取地址栏中参数
function getUrlParam(name) {
let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
const urlStr = window.location.href;
const state = urlStr.includes('?');
if (state) {
let str = urlStr.split('?')[1];
let r = str.match(reg);
if (r != null) return decodeURI(r[2]);
}
return '';
}
console.log(this.getUrlParam('tab'))
console.log(this.getUrlParam('code'))
console.log(this.getUrlParam('status'))
修改指定的地址栏参数
function changeURLArg(url,arg,arg_val){
var pattern=arg+'=([^&]*)';
var replaceText=arg+'='+arg_val;
if(url.match(pattern)){
var tmp='/('+ arg+'=)([^&]*)/gi';
tmp=url.replace(eval(tmp),replaceText);
return tmp;
} else {
if(url.match('[\?]')){
return url+'&'+replaceText;
} else {
return url+'?'+replaceText;
}
}
return url+'\n'+arg+'\n'+arg_val;
}
console.log(changeURLArg('https://juejin.cn/search?query=js%20%E6%94%B9%E5%8F%98%E5%9C%B0%E5%9D%80%E6%A0%8F%E5%9C%B0%E5%9D%80&type=0','type', '9'))
image.png










网友评论