Onhashchange with IE 9

I have the following code

$(document).ready(function() {
   if ("onhashchange" in window) {
      alert("The browser supports the hashchange event!");
   }
   function test(){
  alert("hash has changed = " + window.location.hash)
   }
   window.onhashchange =test;
}

I click a link that changes the hash, and in all other browsers I get a warning in test

However, in IE, I get the first warning that it supports onhashchange, but when the hash changes nothing happens.

Any ideas?

+3
source share
3 answers

Here is an example in the MSDN document page .

Basically, I deleted all the extra “maps” on my page, and the only difference between them and your example is that they include the following meta tag:

<meta http-equiv="X-UA-Compatible" content="IE=8" >

I added this to the head tag in your example and it worked perfectly.

, , IE 8, IE 9 ( -).

+1

:

window.onhashchange?

, IE8, IE7, true "onhashchange" , , document.documentMode.

var docmode = document.documentMode;
if ('onhashchange' in window && (docmode === undefined || docmode > 7 )) {
    window.onhashchange = checkHash;
}
+2

hashchange HTML5 ; , IE8.

IE 9, FF 5, Safari 5 Chrome 12 Win 7:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <script>
            window.onhashchange = doThisWhenTheHashChanges;

            function changeTheHash()
            {
                var newHashValue = document.getElementById("newHashInput").value;
                var currentLocation = window.location.pathname;
                window.location.hash = newHashValue;
            }

            function doThisWhenTheHashChanges()
            {
                alert("The hash has changed!");
            }
        </script>
    </head>
    <body> 
        <h1>Hash Change Test</h1>
        <form>
            <input type="text" id="newHashInput" autofocus>
            <input type="button" value="Set" onclick="changeTheHash()">
        </form>
    </body>
</html>
+1

Source: https://habr.com/ru/post/1791827/


All Articles