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
Note that in case of Internet Explorer 7 and Internet Explorer 9 the if
statment will give true (for "onhashchange" in windows), but the window.onhashchange
will never fire, so it's better to store hash and check it after every 100 millisecond whether it's changed or not for all versions of Internet Explorer.
if (("onhashchange" in window) && !($.browser.msie)) {
window.onhashchange = function () {
alert(window.location.hash);
}
// Or $(window).bind( 'hashchange',function(e) {
// alert(window.location.hash);
// });
}
else {
var prevHash = window.location.hash;
window.setInterval(function () {
if (window.location.hash != prevHash) {
prevHash = window.location.hash;
alert(window.location.hash);
}
}, 100);
}
EDIT -
Since jQuery 1.9, $.browser.msie
is not supported. Source: http://api.jquery.com/jquery.browser/