IE not firing popstate when hashchange happens
Asked Answered
U

3

10

I have page that is doing the routing on clientside, using history API & push/popstate. Which works just fine on all the modern browsers. (search engines will be supported by node.js prerenderer)

However, I recently bumped into issue where IE doesn't fire popstate on hashchange while, while pushstate with urls works just fine, including IE11.

For example, like so...

$(document).on('click', 'a', function(e) {
    e.preventDefault();
    History.pushState({}, '', $(this).attr('href'));
});

...which correctly fires...

$(window).on('popstate', function() {
    console.log('url changed');
});

According the W3C spec, the hashchange should fire popstate as it's changing the current history. However, when I add in hash links (<a href="#hashchange">...), clicking that on IE, nothing fires. :/

I wouldn't want to do IE detecting (as nowadays there are so many browsers which might fall in to the same pit of doom), rather than using feature detection. However, as history (popstate/pushstate) works just fine the rest of the way I can't even detect the issue on missing push/popstate...

if(!window.history || !window.history.pushState) { ...

... and use the hashchange instead. :/

Any thoughts?

PS. As a bonus, using jquery.history.js (jquery wrapped version of history.js) with hashtag url blows the whole thing up.

http://localhost/routetest/index.html#/page1/1234

becomes

http://localhost/page1/1234

... ??? :/

Ung answered 2/5, 2014 at 8:15 Comment(2)
What version of IE are you peeking at? There seem to be a problem with pushState() for IE 9. Look at this question with provided answer: #3723315Staford
Ah, sorry. IE11. Just mentioned modern browsers but failed on the IE version :)Ung
R
2

This is a known issue in IE11 and all Internet Explorer browsers before Edge.

See this link https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/3740423/. The Microsoft response is the last post in this issue report, and notes the newest version that is working.

All versions of IE will display this behavior, and hacking/monkey-patching the correct behavior back into the event framework is probably the only way to make it work reliably. This means you will likely need IE specific logic if you want to implement it yourself.

Rondelle answered 4/1, 2017 at 21:27 Comment(0)
R
1

Try using a polyfill for History API for better support https://github.com/browserstate/history.js

Romberg answered 21/8, 2014 at 20:8 Comment(1)
I don't understand the downvote. It fixes op's problem.Romberg
U
0

In IE 10 and 11 the popstate event will only be fired for a history item after the state has been set with history.pushState or replaceState, including when it is set to null, and only when traversing between two items that have had state set. Therefore, it is necessary to set the state for a new history item in the hashchange event. Once the state is set, the popstate event will fire when the user navigates through the browser history.

https://developer.mozilla.org/en-US/docs/Web/API/History

Unselfish answered 10/3, 2017 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.