JAVASCRIPT: be notified when location hash changes

I have code that should be fired when the URL at the location bar on the browser changes, most specifically, when the “hash” part of the URL changes. This happens when the user clicks a “a” anchor link to the same page for instance. This became very popular due to techniques to ensure browser history works even when just pieces of the page gets loaded through Ajax.

I found out that Firefox 3.6 and Internet Explorer 8 implement an event called  “onhashchange”, following the HTML 5 specifications. If you want to target other browsers, you have to do it yourself using timers or use history management javascript libraries (that probably will use the timer strategy).

Thanks to:

http://stackoverflow.com/questions/680785/on-window-location-hash-change

https://developer.mozilla.org/en/DOM/window.onhashchange

This entry was posted in programming and tagged , , . Bookmark the permalink.

Leave a comment