5

I want to make a page with a lot of Javascript interactions. However, while a user navigates through the page the URL must change too. So, when the user shares the URL or saves it, it can lead him to the actual state he was.

How can I do that?

Examples:

myapp.com/page1

myapp.com/page2

nbro
  • 12,226
  • 19
  • 85
  • 163
Hugo Mota
  • 9,751
  • 9
  • 38
  • 55

4 Answers4

7

pushState, as seen on github

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • 1
    It gracefully degrades in browsers that haven't yet caught up with pushState. – Quentin Jul 06 '11 at 19:44
  • yeah. it's really nice feature! although in this particular case the old technique fits better with my needs, i guess this is, after all, the best choice for this problem. – Hugo Mota Jul 06 '11 at 20:45
4

Answered by this SO question: Change the URL in the browser without loading the new page using JavaScript

Community
  • 1
  • 1
Mark Pope
  • 10,768
  • 10
  • 46
  • 57
2

The only part of the url (or location) that you can change without reloading the page, is the hash. That is the part behind the #. Many ajax enhanced applications make use of this, including Twitter. You can change this hash on the go, and interpret the hash tag on page load to initialize the page to the correct state.

GolezTrol
  • 109,399
  • 12
  • 170
  • 196
  • 1
    Thanks to pushState, that isn't true any more (at least in most browsers). Manipulating the hash like that has [significant drawbacks](http://isolani.co.uk/blog/javascript/BreakingTheWebWithHashBangs) – Quentin Jul 06 '11 at 19:10
  • Well, it is supported in the latest FF and partially in Chrome. Too bad for IE and all mobile browsers, but hey, who uses them. – GolezTrol Jul 06 '11 at 20:00
  • It's fully supported in Chrome, and Opera, Safari has partial support as does Mobile Safari; Opera Mobile and Android Browser support it. – Quentin Jul 06 '11 at 20:01
0

Set this value: window.location.href

  window.location.href = "myapp.com/page2";
Mark Pope
  • 10,768
  • 10
  • 46
  • 57
  • 4
    It will cause the page to be reloaded, which is probably undesirable in the situation described by the OP. – GolezTrol Jul 06 '11 at 19:10