export const changeURLArg = (url, arg, arg_val) => {
// url当前地址window.location.href
// arg传参字段 例如?key={arg_val}
// arg_val传参值
const pattern = `${arg}=([^&]*)`;
const replaceText = `${arg}=${arg_val}`;
if (url.match(pattern)) {
let tmp = `/(${arg}=)([^&]*)/gi`;
tmp = url.replace(eval(tmp), replaceText);
return tmp;
}
if (url.match('[\?]')) {
return `${url}&${replaceText}`;
}
return `${url}?${replaceText}`;
}
//使用replaceState替换当前地址
window.history.replaceState('', '', changeURLArg(window.location.href, 'keyword','aa'));
动态修改地址栏传参数
最新推荐文章于 2022-09-23 17:27:50 发布