[英]Backbone.js PushStates: Fallback for Internet Explorer not working
我的网站刚刚在Backbone.js中实现了pushstate,而整个网站都因IE而中断。 如何为IE创建后备广告?
我正在努力实现的目标
主要网址: http://mydomain.com/explore
: 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
禁用推送状态,如果它是不兼容的浏览器。
但是,要使IE访问在正常浏览器中正常运行的内容( http://mydomain.com/explore
将需要转到http://mydomain.com/explore/#explore
,这使事情变得混乱! 访问http://mydomain.com/explore/1234
更多信息IE需要访问http://mydomain.com/explore/#explore/1234
应该如何解决?
如果您不希望使用http://mydomain.com/explore/#explore
网址,则必须重定向到http://mydomain.com/#explore
以便Backbone以此开头。
if(!pushState && window.location.pathname != "/") {
window.location.replace("/#" + window.location.pathname)
}
UPD:在将路径设置为哈希window.location.pathname.substr(1)
时,可能必须删除斜杠window.location.pathname.substr(1)
UPD2:如果您希望/explore/
作为主干路由的根,则必须将其从路由中排除,并在History.start({root: "/explore/"})
设置为根。
routes: {
'': 'explore',
':id': 'viewListing',
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.