2013-04-05 88 views
4

我正在使用一个ember应用程序(使用ember-1.0.pre.js)。我试图在IE8上提供跨浏览器兼容性。Html4浏览器不支持HTML5历史API的history.pushState和history.replaceState方法

问题是在每次转换后都会生成url,这对用户来说似乎是不正确/错误的。假设我点击了像the_ domain_name/sell/new这样的网址,最初让我在我们的应用程序的销售页面上。然后我试图通过一个叫做“购买”的新状态,这个状态将在我们申请的购买页面上登陆。

新的状态转换在IE8地址栏中生成URL the_ domain_name/sell/new#/offers/purchase?&suid=1365149991779013736531657156165而不是the domain_name/offers/purchase

注: the_domain_name = http://www.example.com

生成的URL包括两个不正确的东西,

  1. 最初的前缀 “/销售/新#”。

  2. url查询字符串中的参数“?& _suid = 1365149991779013736531657156165”。

我试图找出问题,发现HTML4浏览器不支持从HTML5的History API中的pushState和replaceState方法。我如何提供对IE8的支持任何人都可以帮助我呢?

+0

使用Modernizr的检查功能,并使用填充工具对非支持的浏览器。 http://modernizr.com/docs/#polyfills – 2013-04-05 13:41:22

回答

11

我建议History.js作为填充工具的浏览器不支持历史API:https://github.com/browserstate/history.js

它是工作在:

HTML5的浏览器:

  • 火狐4+
  • 铬8+
  • Opera 11.5
  • Safari 5.0及
  • 的Safari的iOS 4.3+

HTML4浏览器:

  • IE 6,7,8,9
  • 火狐3
  • 歌剧10,11.0
  • Safari 4
  • Safari iOS 4.2,4 .1 4.0,3.2

添加​​&注册一个history.js位置处理成灰烬你应用。

下面是我从原来的Ember.HistoryLocationFull code)修改了部分

(function() { 
    var get = Ember.get, set = Ember.set; 
    var popstateFired = false; 
    Ember.HistoryJsLocation = Ember.Object.extend({ 
    initState: function() { 
     this.replaceState(this.formatURL(this.getURL())); 
     set(this, 'history', window.History); 
    }, 
    getState: function() { 
     return get(this, 'history').getState().state; 
    }, 
    pushState: function(path) { 
     History.pushState({ path: path }, null, path); 
    }, 
    replaceState: function(path) { 
     History.replaceState({ path: path }, null, path); 
    } 
    }); 
    Ember.Location.registerImplementation('historyJs', Ember.HistoryJsLocation); 
})(); 

然后在你的应用程序中使用该填充工具:

App.Router.reopen({ 
    location: 'historyJs' 
}); 
+0

这真棒,会使ember.js成为一个很好的补充 - 也许不是核心,但肯定是一个插件。 – 2013-04-05 15:50:43

+0

提醒:如果您想保留相同的标题或实现您自己的标题处理程序,请将'pushState/replaceState'的第二个参数更改为'document.title'。 – inDream 2013-04-07 06:32:49