Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  100] [ 4]  / answers: 1 / hits: 41576  / 15 Years ago, fri, december 18, 2009, 12:00:00

I have a Ajax heavy application that may have a URL such as


http://example.com/myApp/#page=1

When a user manipulates the site, the address bar can change to something like


http://example.com/myApp/#page=5

without reloading the page.


My problem is the following sequence:



  1. A user bookmarks the first URL.

  2. The user manipulates the application such that the second URL is the current state.

  3. The user clicks on the bookmark created in step 1.

  4. The URL in the address bar changes from http://example.com/myApp/#page=5 to http://example.com/myApp/#page=1, but I don't know of a way to detect the change happened.


If I detect a change some JavaScript would act on it.


More From » dom-events

 Answers
25

HTML5 introduces a hashchange event which allows you to register for notifications of url hash changes without polling for them with a timer.


It it supported by all major browsers (Firefox 3.6, IE8, Chrome, other Webkit-based browsers), but I'd still highly suggest to use a library which handles the event for you - i.e. by using a timer in browsers not supporting the HTML5 event and using the event otherwise.


window.onhashchange = function() {
alert("hashtag changed");
};

For further information on the event, see https://developer.mozilla.org/en/dom/window.onhashchange and http://msdn.microsoft.com/en-us/library/cc288209%28VS.85%29.aspx.


[#98037] Tuesday, December 15, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
luna

Total Points: 698
Total Questions: 114
Total Answers: 93

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
luna questions
;