Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How do I change the URL in the address bar without refreshing the page? [duplicate]

Possible Duplicate:
How do I, with JavaScript, change the URL in the browser without loading the new page?

I noticed that web apps like GMail and GrooveShark can change the URL in the address bar of my browser without refreshing the page. Ajax is obviously used to change the content. How can I change the URL?

like image 598
Leo Jiang Avatar asked Oct 23 '11 21:10

Leo Jiang


People also ask

How do I use JavaScript to modify the URL without reloading the page?

the page using JavaScript? Method 2: Adding a new state with pushState() Method: The pushState() method is used to add a new history entry with the properties passed as parameters. This will change the current URL to the new state given without reloading the page.

How do I change the URL of a website without reloading it?

The pushState() method is used to pass the properties in the form of parameters to add the new history entry. It is used to modify the current URL with the new given state without having to reload the page.

What is the correct way of changing the URL of a page using JavaScript?

window. location. replace() replaces the current URL with a new one, whilst overwriting the old URL's entry in the history.

Which method is used to change the address in address bar of the browser in JavaScript?

HTML5 History API allows browsers to change the URL in browser address bar without reloading or refreshing the page using pushState function. The pushState method works similar to window.


2 Answers

Gmail and Grooveshark only change the hash, this is done by changing:

location.hash = 'blah'

If you target HTML5 enabled browsers, you can use window.history.pushState and window.history.popState, see http://spoiledmilk.dk/blog/html5-changing-the-browser-url-without-refreshing-page

like image 65
topek Avatar answered Sep 19 '22 19:09

topek


You may want to look into HTML5 push state. I know iQuery Mobile is using this feature to modify the location URL without triggering a page load and it might be the right solution for you.

https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history

Here is an example of how it works:

window.history.pushState(data, "Title", "/new-url");
like image 28
Nick Clark Avatar answered Sep 16 '22 19:09

Nick Clark