Update browser's URL without reloading the page

Daniel picture Daniel · Aug 16, 2011 · Viewed 10.4k times · Source

Is it possible to change the URL shown in the browser's address bar without having the browser go to that page? Such as, for example, after updating a page's content via an AJAX call?

My understanding is that this is not possible, which is why sites such as twitter and facebook update the hash-tag on ajax calls.

That is until today, when I went on http://8tracks.com/ and started to play with the filter on the right hand side... turning different genres on and off, I noticed that even though it was doing ajax calls to refresh the content on the page, the URL was also being dynamically updated.

Does anyone know how they do this?

(aside, I'm currently using Chrome, but when I went back and looked again with IE9, I noticed that the URL was not being updated.. is this maybe a Chrome only thing?)

Answer

Arnaud Le Blanc picture Arnaud Le Blanc · Aug 16, 2011

This is possible in modern browsers by using the HTML5 History API:

history.pushState(null, null, '/some-path')

See https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#The_pushState%28%29.c2.a0method

This works in Firefox, Chrome, Opera, Safari (not IE).