« Back to Index

Detect onhashchange support

View original Gist on GitHub

Tags: #js

hash.js

var docmode = document.documentMode;

// Does the browser support window.onhashchange?
// Note that IE8 running in IE7 compatibility mode reports true for 'onhashchange' in window, 
// even though the event isn't supported, so also test document.documentMode.
if ('onhashchange' in window && (docmode === undefined || docmode > 7 )) {
	window.onhashchange = checkHash;
} 
// IE7 doesn't support the hashchange event so we fall back to standard polling technique
else {
	poll = window.setInterval(checkHash, 500);
	
	// Clean-up objects as IE7 has hideous performance
	window.onunload = function() {
		window.clearInterval(poll);
	}
}