js删除浏览器地址上指定的参数
解决方法:
如果只想删除某个参数,例如下面刪除名字為title的参数:
var url = window.location.href;
var name = "title";
if (url.indexOf("?") != -1) {
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (results) {
if (new RegExp("&").exec(url)&&results[0].startsWith('?')){
results[0]=results[0].replace('?','');
results[0]+='&'
url = url.replace(results[0].replace(/\+/g, " "), '');
window.history.pushState({}, 0, url);
}
}