【问题标题】:Graceful degradation of HTML5 History APIHTML5 History API 的优雅降级
【发布时间】:2012-12-12 05:35:14
【问题描述】:

我正在使用本机 HTML5 History API 调用,我知道 cross brower compatibility issues,但在我的特定用例中,这些并不重要。对于我有限的需求,浏览器的行为始终如一,上面链接中的表格讲述了同样的故事。所以我真的不想在页面上添加另一个 JS 库(History.js),但我绝对希望它在不支持它们的浏览器中优雅地失败。我不希望他们抛出 JS 错误。

例如,IE(我想其他不支持的浏览器也会)抛出这个 JS 错误:Object doesn't support property or method 'replaceState'

我试过这样做,但它不起作用:

window.history = window.history || {};
if (jQuery.isEmptyObject(window.history)) {
    window.history.prototype.replaceState = function(a,b,c) {};
    window.history.prototype.pushState = function(a,b,c) {};
}

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    检查pushState 是否存在于历史对象中。

    if( !(window.history && 'pushState' in window.history) )
    

    【讨论】:

    • 只测试过一次,谢谢!接受并赞成:)顺便说一句,我将答案编辑为更好的示例版本。
    猜你喜欢
    • 2011-01-15
    • 2011-08-06
    • 2011-08-29
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 2013-01-18
    相关资源
    最近更新 更多