admin管理员组文章数量:1134605
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 with jQuery.support
...
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 with jQuery.support
...
- 4 jQuery hashchange event - jQuery plugin works perfect, even in IE8. + it's very easy to use it :) – enloz Commented Sep 16, 2011 at 3:38
12 Answers
Reset to default 71You 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
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.
There is a hashchange plug-in which wraps up the functionality and cross browser issues available here.
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.
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;
I just ran into the same problem (lack of hashchange event in IE7). A workaround that suited for my purposes was to bind the click event of the hash-changing links.
<a class='hash-changer' href='#foo'>Foo</a>
<script type='text/javascript'>
if (("onhashchange" in window) && !($.browser.msie)) {
//modern browsers
$(window).bind('hashchange', function() {
var hash = window.location.hash.replace(/^#/,'');
//do whatever you need with the hash
});
} else {
//IE and browsers that don't support hashchange
$('a.hash-changer').bind('click', function() {
var hash = $(this).attr('href').replace(/^#/,'');
//do whatever you need with the hash
});
}
</script>
Note that in case of IE 7 and IE 9 if statment will give true for ("onhashchange" in windows) but the window.onhashchange will never fire, so its better to store hash and check it after every 100 millisecond whether its changed or not for all versions of IE.
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);
}
What about using a different way instead of the hash event and listen to popstate like.
window.addEventListener('popstate', function(event)
{
if(window.location.hash) {
var hash = window.location.hash;
console.log(hash);
}
});
This method works fine in most browsers i have tried so far.
this tiny jQuery plugin is very simple to use: https://github.com/finnlabs/jquery.observehashchange/
I think Chris Coyier has solution for that hashing problem, have a look at his screencast:
Best Practices with Dynamic Content
Use Modernizr for detection of feature capabilities. In general jQuery offers to detect browser features: http://api.jquery.com/jQuery.support/. However, hashchange is not on the list.
The wiki of Modernizr offers a list of libraries to add HTML5 capabilities to old browsers. The list for hashchange includes a pointer to the project HTML5 History API, which seems to offer the functionality you would need if you wanted to emulate the behavior in old browsers.
Here is updated version of @johnny.rodgers
Hope helps someone.
// ie9 ve ie7 return true but never fire, lets remove ie less then 10
if(("onhashchange" in window) && navigator.userAgent.toLowerCase().indexOf('msie') == -1){ // event supported?
window.onhashchange = function(){
var url = window.location.hash.substring(1);
alert(url);
}
}
else{ // event not supported:
var storedhash = window.location.hash;
window.setInterval(function(){
if(window.location.hash != storedhash){
storedhash = window.location.hash;
alert(url);
}
}, 100);
}
本文标签: javascriptjQueryhashchange eventStack Overflow
版权声明:本文标题:javascript - jQuery - hashchange event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736821259a1954292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论