PanBou
PanBou

Reputation: 41

Keep original url for pushstate

I use the following function:

$( 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);

});

I need somehow at each ajax completion to change original url and not previously changed url through pushState.

With the function above at first ajax completion pushState does the job but on next ajax completion pushState decodedUri2 tries to replace from changed url but i need every time to change the original url and pushState the result. Is that possible?

Any ideas of achieving this?

Thank you!

EDIT: Rephrase of the question. Each time an ajax completes, some attributes are added to the URL and i need to replace those attributes. And again the same with each ajax request. So actually i need somehow each time an ajax completion is done to pushState the current URL after ajax completion but from original state.

EXAMPLE: http://ocdemo.eu/desktops.

If you select filters from left column each time url is changing with ajax. So each time i need the shown url to be replaced with something else

Upvotes: 3

Views: 1808

Answers (2)

Ross Newby
Ross Newby

Reputation: 103

For anyone searching this, who want to apply pushState without updating the page URL. You can omit the param entirely, as stated in the docs:

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

Both will work:

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

If you're looking to change a URL param on AJAX call in the current URL, theres plenty of discussion on the thread:

How to replace url parameter with javascript/jquery?

Upvotes: 2

caisah
caisah

Reputation: 2087

You would have to capture that url to a variable and use that:

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);

});

Upvotes: 1

Related Questions