删除指定链接上的参数
程序员文章站
2024-02-18 19:47:46
...
// 删除指定链接上的参数
function urlDel(url,name){
var urlArr = url.split('?');
if(urlArr.length>1 && urlArr[1].indexOf(name)>-1){
var query = urlArr[1];
var obj = {}
var arr = query.split("&");
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].split("=");
obj[arr[i][0]] = arr[i][1];
};
delete obj[name];
console.log(obj,'-------')
if(JSON.stringify(obj) != "{}") { // 判断obj还有值没
var urlte = urlArr[0] +'?'+ JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");
} else {
var urlte = urlArr[0];
}
return urlte;
}else{
return url;
};
}
比如:http://api.hvfoxkart.com/hwhs/index.html?code=1
//执行后
const urls = urlDel(window.location.href,'code');
console.log(urls)
http://api.hvfoxkart.com/hwhs/index.html