【发布时间】:2014-06-18 20:55:57
【问题描述】:
我的页面正在客户端进行路由,使用历史 API 和推送/弹出状态。在所有现代浏览器上都可以正常工作。 (node.js prerenderer 将支持搜索引擎)
但是,我最近遇到了一个问题,即 IE 不会在 hashchange 上触发 popstate,而带有 urls 的 pushstate 工作得很好,包括 IE11。
例如,像这样……
$(document).on('click', 'a', function(e) {
e.preventDefault();
History.pushState({}, '', $(this).attr('href'));
});
...正确触发...
$(window).on('popstate', function() {
console.log('url changed');
});
根据 W3C 规范,hashchange 应该在改变当前历史记录时触发 popstate。但是,当我添加哈希链接 (<a href="#hashchange">...) 时,在 IE 上单击它,没有任何反应。 :/
我不想进行 IE 检测(因为现在有很多浏览器可能会落入同一个厄运深渊),而不是使用特征检测。但是,由于历史记录(popstate/pushstate)在剩下的路上工作得很好,我什至无法检测到缺少 push/popstate 的问题......
if(!window.history || !window.history.pushState) { ...
... 并改用 hashchange。 :/
有什么想法吗?
PS。作为奖励,使用带有主题标签 url 的 jquery.history.js(jquery 包装的 history.js 版本)会炸毁整个事情。
http://localhost/routetest/index.html#/page1/1234
变成
http://localhost/page1/1234
... ??? :/
【问题讨论】:
-
你在看什么版本的IE?对于 IE 9,
pushState()似乎存在问题。请查看此问题并提供答案:stackoverflow.com/questions/3722815/… -
啊,对不起。 IE11。刚刚提到现代浏览器,但在 IE 版本上失败了:)
标签: javascript jquery pushstate history.js