当hashchange发生时,IE不会触发popstate [英] IE not firing popstate when hashchange happens

查看:166
本文介绍了当hashchange发生时,IE不会触发popstate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页正在客户端进行路由,使用历史记录API&推/ popstate。这适用于所有现代浏览器。 (node.js prerenderer将支持搜索引擎)

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)

然而,我最近碰到了一个问题,即IE不会在hashchange上触发popstate,而pushstate with urls工作很好,包括IE11。

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');
});

根据W3C规范,hashchange应该触发popstate,因为它正在改变当前历史记录。但是,当我添加哈希链接(< a href =#hashchange> ... )时,在IE上单击它,什么都不会触发。 :/

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. :/

我不想做IE检测(因为现在有很多浏览器可能会陷入同样的​​厄运),而不是使用功能检测。但是,由于历史(popstate / pushstate)在剩余的方式下工作得很好,我甚至无法检测到丢失的push / popstate上的问题......

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) { ...

...而是使用hashchange代替。 :/

... and use the hashchange instead. :/

有什么想法吗?

PS。作为奖励,使用带有#标签url的jquery.history.js(jquery包装的history.js版本)可以打破整个过程。

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

变为

http://localhost/page1/1234

...... ??? :/

... ??? :/

推荐答案

这是IE11和Edge之前的所有Internet Explorer浏览器中的已知问题。

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

请参阅此链接 https:/ /developer.microsoft.com/en-us/microsoft-edge/platform/issues/3740423/ 。 Microsoft回复是此问题报告中的最后一篇文章,并注意到最新版本正在运行。

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.

IE的所有版本都会显示此行为,并且将正确的行为修复/猴子修补回事件框架可能是使其可靠运行的唯一方法。这意味着如果你想自己实现它,你可能需要IE特定的逻辑。

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.

这篇关于当hashchange发生时,IE不会触发popstate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆