从Url Javascript中删除附属查询字符串

从Url Javascript中删除附属查询字符串,javascript,pushstate,Javascript,Pushstate,在我的购物车中,我创建了如下联盟URL: http://mysite.local/phones-and-pdas/iphone?z=5509d173cffeb 我想使用history.pushState删除从?开始的查询字符串 我尝试过使用slice和split,但这似乎也会影响其他URL,即使它们不包含?z= 例如,当我转到: http://mysite.local/account/dashboard 地址url更改为: http://mysite.local/account/dashbo

在我的购物车中,我创建了如下联盟URL:

http://mysite.local/phones-and-pdas/iphone?z=5509d173cffeb
我想使用history.pushState删除从?开始的查询字符串

我尝试过使用slice和split,但这似乎也会影响其他URL,即使它们不包含?z=

例如,当我转到:

http://mysite.local/account/dashboard
地址url更改为:

http://mysite.local/account/dashboar
需要注意的是,上面的代码在附属链接上确实非常有效

我确信这是一个简单的调整,但我在搜索时找不到具体的答案。

首先添加一个检查,看看是否有?z=there:

var href = window.location.href;
if(href.indexOf('?z=')) {
    var url  = href.slice(0, href.indexOf('?z='));
    history.pushState(null, null, url);
}
首先添加一个检查,查看?z=是否存在:

var href = window.location.href;
if(href.indexOf('?z=')) {
    var url  = href.slice(0, href.indexOf('?z='));
    history.pushState(null, null, url);
}
看而不是看

href.split“?”[0]应该可以使用。

查看而不是查看

function trackingLink() {
var href = window.location.href;
var url  = href.split('?z=');
history.pushState(null, null, url[0]);
}

href.split“?”[0]应该可以使用。

只需对+DaveS的答案进行说明:

function trackingLink() {
var href = window.location.href;
var url  = href.split('?z=');
history.pushState(null, null, url[0]);
}
var href = window.location.href;
var url = href.split("?")[0];
history.pushState(null, document.title, url);

我只想听听埃拉博特对戴维的回答:

var href = window.location.href;
var url = href.split("?")[0];
history.pushState(null, document.title, url);

太好了,谢谢。我试着分开,但我做错了。谢谢你的帮助,太好了,谢谢。我试着分开,但我做错了。谢谢你的帮助。