IE中的骨干路由器

Squ*_*kle 6 internet-explorer backbone.js backbone-routing

我正在使用Backbone.js路由器在用户点击某些URL路由时触发某些初始化方法.所以/posts/1通过一个vanilla锚标签应该触发/posts/:id我的Backbone路由器中与之关联的任何回调.这在Backbone.history.start({ pushState : true })设置时在现代浏览器中工作正常.但是,在IE中,尝试命中的用户/posts/1将被重定向到/#posts/1,这只是我的主页,其中包含无意义的哈希字符串.

要清楚,我不需要pushState.我不是试图 URL 送到浏览器历史记录.我只是想读取它们,然后触发回调,这在任何浏览器中都应该是可行的.

看起来像简单的功能,但我很难过.

谢谢!

Squ*_*kle 7

我可以在这里回答我自己的问题.我需要的功能类型可以通过以下方式实现:

var suffix_pattern = new RegExp('\/?' + config.history_root + '\/?','i');

// if IE
if (!Modernizr.history) {

    // initialize router/Backbone.history, but turn off route parsing,
    // since non-window.history parsing will look for a hash, and not finding one,
    // will break our shit.
    Backbone.history.start({ silent: true, hashChange: true });

    // convert any post-# elements to a standard URL to be parsed by our router
    var subroute = window.location.hash.replace('#', '/').split('?')[0],
           route = window.location.pathname.replace(suffix_pattern, '') + subroute;

    Backbone.history.loadUrl(route);
} else {
    Backbone.history.start({ pushState: true, silent: true });
    Backbone.history.loadUrl(Backbone.history.getFragment().replace(suffix_pattern, '').split('?')[0]);
}
Run Code Online (Sandbox Code Playgroud)

  • 我的生产版本删除了所有评论. (2认同)