Update URL on AJAX call?

dougoftheabaci picture dougoftheabaci · Jul 15, 2009 · Viewed 50.9k times · Source

Right now the biggest issue I'm having with using AJAX is the fact that if I use AJAX on a page, go to another page, then use the browser's back button to go back anything that was changed with AJAX is gone.

I've thought about using the jQuery Addresss plugin to solve that problem but I don't like how it only amends the URL with "#whatever.html" instead of changing it completely.

Ideally, what I would like is to have the URL go from: "www.example.com/p:2/" to "www.example.com/p:3/" when I make the relevant AJAX call.

Is this at all possible?

Answer

Aleksey Razbakov picture Aleksey Razbakov · Jan 25, 2011

It's possible with HTML5. You can test as example GitHub or Vkontakte site.

The best answer is here: Change the URL in the browser without loading the new page using JavaScript

It says that you can use history.pushState function for those purposes. But this solution will only work in HTML5 compatitable browsers. Otherwise you need to use hash-method.