5

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?

amateur
  • 43,371
  • 65
  • 192
  • 320

2 Answers2

8

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

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • 2
    IE8 was the first IE browser to support it natively. Beware, however, that IE8 in Compatibility Mode (e.g., acting as IE7) will claim to support it, but it does not, as Ben Alman points out: http://benalman.com/projects/jquery-hashchange-plugin/ – David Lantner Feb 07 '12 at 19:45
0

[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>
Community
  • 1
  • 1
johnny.rodgers
  • 1,347
  • 1
  • 11
  • 12
  • 1
    just wanted to point out that $.browser doesn't work anymore since jQuery version 1.9 as it is now deprecated. http://api.jquery.com/jQuery.browser/ – Rumplin Oct 30 '13 at 07:58