I am using history.pushState to append few params to current page URL after making an AJAX call on my page. Now on same page based on user action, I want to update the page URL again with same or additional set of params. So my code looks like this:
var pageUrl = window.location.href + "?" + queryString;
window.history.pushState('','',pageUrl);
queryString is my list of query params.
- For example, My Default page URL: http://sample.com/
- After First AJAX call on same page URL should be: http://sample.com?param1=foo¶m2=bar
- After Second AJAX call on same page URL can be: http://sample.com/?param1=foo,foo1¶m2=bar¶m3=another_foo
But with the above code my params are getting appended to URL with the params and they look like below after second AJAX call:
http://sample.com?param1=foo¶m2=bar¶m1=foo,foo1¶m2=bar¶m3=another_foo
So the params appear twice in the URL, is there any way of replacing the params in URL before pushing to History or any other better way to achieve this in javascript(jquery) ?