js去除原本url多余的页面栏参数并不进行页面跳转

例如‘http://test.codelovers.cn/?_t=1539913651’实现的效果是自动去掉后缀包含?部分,实现代码如下if(top != self){

	top.location=self.location;
}else{
	var url = location.search;
	if(url){
		var old_url = window.location.href;
		var new_url = old_url.substring(0, old_url.indexOf('?'));
		self.location = new_url;//1.当前直接替换了路径到url,实际上还是会进行跳转
        history.replaceState(null,null, new_url); //2.替换地址栏,直接将地址栏参数替换
} }

js内容为正则替换掉参数内容,没有参数时,清空参数,有参数时替换参数内容。
转自:https://www.codelovers.cn/article/20181226091635.html与https://blog.csdn.net/weixin_42403306/article/details/105861346
 
posted @ 2020-11-09 11:32  lxj666  阅读(1268)  评论(0)    收藏  举报