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?)
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With