Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update browser's URL without reloading the page

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

like image 726
Daniel Avatar asked Aug 16 '11 11:08

Daniel


People also ask

How do I change URL without loading page?

replaceState() method The replaceState() is another method that updates the URL without reloading the page. It works exactly the same way as pushState() , but replaces the existing browser history entry instead of adding a new one.

How do I change my Web browser URL?

replace() : // Current URL: https://my-website.com/page_a const nextURL = 'https://my-website.com/page_b'; // This will create a new entry in the browser's history, reloading afterwards window. location. href = nextURL; // This will replace the current entry in the browser's history, reloading afterwards window.

How do you edit HTML without reloading?

You should use the javascript function document. getElementById (DIV THAT YOU WOULD LIKE TO PLACE IN). innerHTML = YOUR_TEXT;. If you need a response from the server without reloading the page (you will still have to load, but the client will not notice), try using AJAX.


1 Answers

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

like image 166
Arnaud Le Blanc Avatar answered Oct 19 '22 22:10

Arnaud Le Blanc