Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect changes in location hash?

I am using Ajax and hash for navigation.

Is there a way to check if the window.location.hash changed like this?

http://example.com/blah#123 to http://example.com/blah#456

It works if I check it when the document loads.

But if I have #hash based navigation it doesn't work when I press the back button on the browser (so I jump from blah#456 to blah#123).

It shows inside the address box, but I can't catch it with JavaScript.

like image 913
MilMike Avatar asked Mar 25 '09 09:03

MilMike


People also ask

How does location hash work?

The hash property of the Location interface returns a string containing a '#' followed by the fragment identifier of the URL — the ID on the page that the URL is trying to target. The fragment is not percent-decoded. If the URL does not have a fragment identifier, this property contains an empty string, "" .

What is Onhashchange?

The onhashchange event occurs when there has been changes to the anchor part (begins with a '#' symbol) of the current URL. An example of what an anchor part actually is: Assume that the current URL is. http://www.example.com/test.htm#part2 - The anchor part of this URL would be #part2.

What is jQuery Hashchange?

The jQuery Mobile . hashchange() event handler enables very basic bookmarkable #hash history by providing a callback function bound to the window. onhashchange event. The onhashchange event fires when a window's hash changes. In browsers that support it, the native HTML5 window.


2 Answers

The only way to really do this (and is how the 'reallysimplehistory' does this), is by setting an interval that keeps checking the current hash, and comparing it against what it was before, we do this and let subscribers subscribe to a changed event that we fire if the hash changes.. its not perfect but browsers really don't support this event natively.


Update to keep this answer fresh:

If you are using jQuery (which today should be somewhat foundational for most) then a nice solution is to use the abstraction that jQuery gives you by using its events system to listen to hashchange events on the window object.

$(window).on('hashchange', function() {   //.. work .. }); 

The nice thing here is you can write code that doesn't need to even worry about hashchange support, however you DO need to do some magic, in form of a somewhat lesser known jQuery feature jQuery special events.

With this feature you essentially get to run some setup code for any event, the first time somebody attempts to use the event in any way (such as binding to the event).

In this setup code you can check for native browser support and if the browser doesn't natively implement this, you can setup a single timer to poll for changes, and trigger the jQuery event.

This completely unbinds your code from needing to understand this support problem, the implementation of a special event of this kind is trivial (to get a simple 98% working version), but why do that when somebody else has already.

like image 84
6 revs, 3 users 89% Avatar answered Oct 23 '22 05:10

6 revs, 3 users 89%


HTML5 specifies a hashchange event. This event is now supported by all modern browsers. Support was added in the following browser versions:

  • Internet Explorer 8
  • Firefox 3.6
  • Chrome 5
  • Safari 5
  • Opera 10.6
like image 28
Miles Avatar answered Oct 23 '22 03:10

Miles