保留pushstate

时间:2017-03-12 14:47:50

标签: javascript jquery pushstate

我使用以下功能:

$( document ).ajaxComplete(function() {
     var currenturl = window.location.href;
     var decodedUri = decodeURIComponent(currenturl);

    var decodedUri2 =decodedUri.replace("&manufacturer[]=","/").replace("&page=","/").replace("&sort=","/").replace("&order=","/");
     history.pushState("", "", decodedUri2);

});

我需要在每次ajax完成时以某种方式更改原始网址,而不是之前通过pushState更改网址。

首先使用上面的函数ajax完成pushState完成作业,但是在下一个ajax完成时,pushState decodeUri2尝试从更改的url中替换,但我每次都需要更改原始url和pushState结果。这可能吗?

实现这一目标的任何想法?

谢谢!

编辑: 问题的改写。每次ajax完成时,一些属性都会添加到URL中,我需要替换这些属性。每个ajax请求再次相同。所以实际上我每次都需要以某种方式完成ajax完成,以便在ajax完成之后将当前URL推送到原始状态。

示例http://ocdemo.eu/desktops

如果您每次使用ajax更改网址时从左列中选择过滤器。因此每次我需要将显示的url替换为其他内容

2 个答案:

答案 0 :(得分:1)

对于搜索此内容的任何人,他们想要应用pushState而不更新页面URL。您可以完全忽略该参数,如文档所述:

https://developer.mozilla.org/en-US/docs/Web/API/History/pushState

两者都能工作:

history.pushState("", "");
history.pushState("", "", "");

如果您希望在当前URL中更改AJAX调用的URL参数,则有关该线程的讨论很多:

How to replace url parameter with javascript/jquery?

答案 1 :(得分:0)

您必须将该网址捕获到变量并使用:

var initialUrl;

$( document ).ajaxComplete(function() {
     var currenturl = window.location.href;
     if (!initialUrl) {
       initialUrl = decodeURIComponent(currenturl);
     }

    var decodedUri2 =initialUrl.replace("&manufacturer[]=","/").replace("&page=","/").replace("&sort=","/").replace("&order=","/");
     history.pushState("", "", decodedUri2);

});