2012-11-06 23 views
6

我的网站刚刚在Backbone.js中实现了pushstates,并且整个网站都停止了IE。我应该如何为IE创建一个回退?Backbone.js PushStates:Internet Explorer不能正常工作

我想实现

主要网址:http://mydomain.com/explore

另一个网址:'http://mydomain.com/explore/1234

该网站的主页是http://mydomain.com/explore触发路由器功能explore

当用户访问http://mydomain.com/explore/1234,骨干路由器将触发功能viewListing,这是一样的功能explore,还包括项目ID 1234细节。

Backbone.js的路由器

// Router 
var AppRouter = Backbone.Router.extend({ 
    routes: { 
     'explore': 'explore', 
     'explore/:id': 'viewListing', 
    }, 

    explore: function() { 
     this.listingList = new ListingCollection(); 
     // More code here 
    }, 

    viewListing: function(listing_id) { 
     this.featuredListingId = listing_id; // Sent along with fetch() in this.explore() 
     this.explore(); 
    } 
}); 

App = new AppRouter(); 

// Enable pushState for compatible browsers 
var enablePushState = true; 

// Disable for older browsers (IE8, IE9 etc) 
var pushState = !!(enablePushState && window.history && window.history.pushState); 

if(pushState) { 
    Backbone.history.start({ 
     pushState: true, 
     root: '/' 
    }) 
} else { 
    Backbone.history.start({ 
     pushState: false, 
     root: '/' 
    }) 
} 

问题:正如你可以在上面的代码中看到,我试图用pushState: false禁用pushstates如果它是不兼容的浏览器。

但是,IE浏览器访问正常的浏览器(http://mydomain.com/explore)通常工作,IE浏览器将需要去http://mydomain.com/explore/#explore这是让事情混乱!更多访问http://mydomain.com/explore/1234 IE需要去http://mydomain.com/explore/#explore/1234

这应该如何解决?

回答

2

如果你不想要http://mydomain.com/explore/#explore的网址,那么你必须重定向到 http://mydomain.com/#explore,所以骨干会以它为起点。

if(!pushState && window.location.pathname != "/") { 
    window.location.replace("/#" + window.location.pathname) 
} 

UPD:你可能不得不设置路径时,作为哈希window.location.pathname.substr(1)

UPD2删除前导斜线:如果你想/explore/成为你的主干航线的根,那么你已经将它排除在外从路线和在History.start({root: "/explore/"})

routes: { 
    '': 'explore', 
    ':id': 'viewListing', 
} 
+0

由于设定为根,我尝试这样做,它重定向(如预期)从'的http:// mydomain.com/explore'到'http://mydomain.com/#/explore'。然而,'http:// mydomain.com'是欢迎/启动页面,而不是应用程序页面('http:// mydomain.com/explore') – Nyxynyx

+0

然后你可能不得不从你的Backbone路线中删除'/ explore /' ,将其设置为Backbone.history.start({root:'/ explore /'})'中的根。 –