cra*_*ish 10 javascript jquery history.js pushstate
我有使用history API和push/popstate在客户端进行路由的页面.这适用于所有现代浏览器.(node.js prerenderer将支持搜索引擎)
但是,我最近碰到了一个问题,IE不会在hashchange上激活popstate,而pushstate与url的工作正常,包括IE11.
例如,像这样......
$(document).on('click', 'a', function(e) {
e.preventDefault();
History.pushState({}, '', $(this).attr('href'));
});
Run Code Online (Sandbox Code Playgroud)
...正确地发射......
$(window).on('popstate', function() {
console.log('url changed');
});
Run Code Online (Sandbox Code Playgroud)
根据W3C规范,hashchange应该触发popstate,因为它正在改变当前的历史记录.但是,当我添加哈希链接(<a href="#hashchange">...),在IE上单击它时,什么都没有激发.:/
我不想做IE检测(因为现在有很多浏览器可能会陷入同样的厄运),而不是使用特征检测.但是,由于历史(popstate/pushstate)在其余部分工作得很好,我甚至无法检测到丢失的push/popstate上的问题......
if(!window.history || !window.history.pushState) { ...
Run Code Online (Sandbox Code Playgroud)
...而是使用hashchange代替.:/
有什么想法吗?
PS.作为奖励,使用jquery.history.js(jquery包装的history.js版本)和hashtag url将整个事情搞砸了.
http://localhost/routetest/index.html#/page1/1234
Run Code Online (Sandbox Code Playgroud)
变
http://localhost/page1/1234
Run Code Online (Sandbox Code Playgroud)
...... ??? :/