I'm having trouble getting the hashchange event to trigger consistently in IE10 and IE11
If I use history.pushState to alter the current hash and then manipulate the hash in the url, then hashchange will be triggered once.
Then if the above is repeated the hashchange is not triggered
I've created a jsbin for testing this issue. To replicate the issue in IE10/IE11, simply click on a section link (e.g. section 4) and then manipulate the section id in the url (e.g. section-3). A hashchange should be triggered but if you repeat, the second time it won't.
http://jsbin.com/locor/5
BTW - this works perfectly in Chrome and Firefox
The hashchange event is fired when the fragment identifier of the URL has changed (the part of the URL beginning with and following the # symbol).
The jQuery Mobile . hashchange() event handler enables very basic bookmarkable #hash history by providing a callback function bound to the window. onhashchange event. The onhashchange event fires when a window's hash changes. In browsers that support it, the native HTML5 window.
Have a repro below. Seems that if you change the hash with pushState IE ignores that change when checking for hashchange events. So if the sequence of your hashes is:
IE compares #3 against #1 instead #2. Since #1 === #3, IE does not fire a hashchange event.
<script>
function log(message) {
var div = document.getElementById("log");
div.innerHTML += message + "<br>";
}
window.addEventListener("hashchange", function () {
log("hashchange");
});
window.addEventListener("popstate", function(){
log("popstate");
});
</script>
<p>1. Manually set the hash in the address bar to "#".</p>
<p><a onclick='history.pushState({}, "", "#somePushState");' style="color:blue;">2. Click here to run push state.</a></p>
<p>3. Manually set the hash in the address bar to "#".</p>
<br>
<p>Expected: browser fires hashchange event for #1 and #3. Actual: IE does not fire hashchange event for #3.</p>
<div id="log"><b>Log:</b><br></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With