jQuery - hashchange event
Solution 1
You can detect if the browser supports the event by:
if ("onhashchange" in window) {
//...
}
See also:
- Detecting event support without browser sniffing
- Emulating
onhashchange
without setInterval window.onhashchange
Solution 2
An updated answer here as of 2017, should anyone need it, is that onhashchange
is well supported in all major browsers. See caniuse for details. To use it with jQuery no plugin is needed:
$( window ).on( 'hashchange', function( e ) {
console.log( 'hash changed' );
} );
Occasionally I come across legacy systems where hashbang URL's are still used and this is helpful. If you're building something new and using hash links I highly suggest you consider using the HTML5 pushState API instead.
Solution 3
There is a hashchange plug-in which wraps up the functionality and cross browser issues available here.
Solution 4
A different approach to your problem...
There are 3 ways to bind the hashchange event to a method:
<script>
window.onhashchange = doThisWhenTheHashChanges;
</script>
Or
<script>
window.addEventListener("hashchange", doThisWhenTheHashChanges, false);
</script>
Or
<body onhashchange="doThisWhenTheHashChanges();">
These all work with IE 9, FF 5, Safari 5, and Chrome 12 on Win 7.
Solution 5
try Mozilla official site: https://developer.mozilla.org/en/DOM/window.onhashchange
cite as follow:
if ("onhashchange" in window) {
alert("The browser supports the hashchange event!");
}
function locationHashChanged() {
if (location.hash === "#somecoolfeature") {
somecoolfeature();
}
}
window.onhashchange = locationHashChanged;
Ian Herbert
Updated on October 11, 2020Comments
-
Ian Herbert over 3 years
I am using:
$(window).bind( 'hashchange', function(e) { });
to bind a function to the hash change event. This seems to work in IE8, Firefox and Chrome, but not in Safari and I assume not in earlier version of IE. For these browsers, I want to disable my JavaScript code that uses the hash and
hashchange
event.Is there a way with jQuery that i can detect if the browser supports the
hashchange
event? Maybe something withjQuery.support
... -
Ian Herbert almost 14 yearsThank you, I will look into that.
-
Ian Herbert almost 14 yearsThanks for that and for the quick response.
-
Vikas about 13 yearsNote that IE8 running in IE7 compatibility mode reports true for 'onhashchange' in window, even though the event isn't supported -from jQuery Mobile
-
Dwarf almost 13 yearsIsn't this too much for the browser to handle? to poll for a hash change every 100ms?
-
enloz over 12 yearsyour sample code made my IE8 alerting until i opened task manager and killed process :)
-
Nick over 10 yearsthat's because there's a typo, instead of "storedHash" use "prevHash" and it will work. He basically used a different variable name from how it's been declared.
-
Arturo Torres Sánchez over 8 yearsPopstate is even newer than hashchange. For instance, it's not supported in IE < 10.
-
James Westgate about 8 yearsOnly required for < IE8
-
tobymackenzie over 7 yearsyou could use
$('a[href^="#"]')
to get links tohref
s beginning with a hash, avoiding the need for a class add -
Daniel Dewhurst about 6 yearsThis works well, use
window.location.hash
to access the current hash.