Skip to content

Instantly share code, notes, and snippets.

@yeyuguo
Last active May 23, 2022 04:55
Show Gist options
  • Save yeyuguo/82aee0f82b393c4eaebbc39a5bb2a91a to your computer and use it in GitHub Desktop.
Save yeyuguo/82aee0f82b393c4eaebbc39a5bb2a91a to your computer and use it in GitHub Desktop.
不刷新页面,清除url参数
/** 不刷新页面, 清除 url 参数 */
function clearAutoParams(name) {
try {
// 获取正则结果
const getRegRes = function(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var result = window.location.search.substr(1).match(reg);
return result
}
const result = getRegRes(name)
if(result && result.length) {
if(history && history.replaceState) {
history.replaceState("", "去除自动领取参数", location.href.replace(result[0],''))
}
}
} catch (error) {
console.log('error: ', error);
}
}
// 清除登陆自动领取参数, 但不刷新页面
// 使用 vue 的 $route.query.xxx = '' 置空无效
// todo 有bug问题: pushState 会更新历史记录,如果返回上一页是无法做到的;
// 应该使用 replaceState,其他都一样,只是不会更新 历史记录; https://blog.csdn.net/drothyChild/article/details/78849852
function clearAutoParams(name) {
// 获取正则结果
const getRegRes = function(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var result = window.location.search.substr(1).match(reg);
return result
}
const result = getRegRes(name)
if(result && result.length) {
if(history && history.pushState) {
history.pushState("", "去除自动领取参数", location.href.replace(result[0],''))
}
}
}
@yeyuguo
Copy link
Author

yeyuguo commented May 23, 2022

todo 有bug问题: pushState 会更新历史记录,如果返回上一页是无法做到的;
应该使用 replaceState,其他都一样,只是不会更新 历史记录; https://blog.csdn.net/drothyChild/article/details/78849852

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment