admin管理员组

文章数量:1302380

I am experiencing some issues binding to the hashchange event in Internet Explorer 7. All other versions of Internet Explorer - ie. 8 & 9 work without issue.

My code is:

 $(window).bind('hashchange', function (e) { alert('hash changed'); });

When the hash of the url changes in Firefox, IE8, IE9 I get the alert box, but in IE7, nothing happens.

Anyone experience this before?

I am experiencing some issues binding to the hashchange event in Internet Explorer 7. All other versions of Internet Explorer - ie. 8 & 9 work without issue.

My code is:

 $(window).bind('hashchange', function (e) { alert('hash changed'); });

When the hash of the url changes in Firefox, IE8, IE9 I get the alert box, but in IE7, nothing happens.

Anyone experience this before?

Share Improve this question asked Jul 26, 2011 at 16:50 amateuramateur 44.7k71 gold badges196 silver badges324 bronze badges 1
  • 7 Internet Explorer is not a valid browser... – genesis Commented Jul 26, 2011 at 16:52
Add a ment  | 

2 Answers 2

Reset to default 8

Pretty sure IE6 and IE7 don't support it natively. Did you try using Ben Alman's jquery BBQ script which fixes this?

[Copying this answer from jQuery - hashchange event ]

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>

本文标签: javascriptbinding hashchange event in IE7 issueStack Overflow