Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE not firing popstate when hashchange happens

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

... ??? :/

like image 439
crappish Avatar asked May 02 '14 08:05

crappish


People also ask

How do you trigger a Popstate?

The popstate event will be triggered by doing a browser action such as a click on the back or forward button (or calling history. back() or history. forward() in JavaScript). Browsers tend to handle the popstate event differently on page load.

Which event is fired when history of browser window changes?

A popstate event is dispatched to the window every time the active history entry changes. If the history entry being activated was created by a call to pushState or affected by a call to replaceState , the popstate event's state property contains a copy of the history entry's state object.


2 Answers

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.

like image 198
havocheavy Avatar answered Oct 17 '22 09:10

havocheavy


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

like image 34
konsumer Avatar answered Oct 17 '22 09:10

konsumer