【问题标题】:Force History.js to use hash URL fallback强制 History.js 使用哈希 URL 回退
【发布时间】:2012-02-08 16:09:07
【问题描述】:

是否可以强制 History.js - https://github.com/browserstate/History.js/ - 在确实支持 HTML5/history API 的浏览器中使用哈希 URL?

这仅用于本地测试,因此如果需要,可以修改 History.js 源代码。

【问题讨论】:

    标签: javascript html history.js


    【解决方案1】:

    我想为测试目的做同样的事情,最终更新了 jquery.history.js 库中的以下行。

    原文:(View on GitHub)

    m.emulated={pushState:!Boolean(a.history&&a.history.pushState&&a.history.replaceState&&!/ Mobile\/([1-7][a-z]|(8([abcde]|f(1[0-8]))))/i.test(e.userAgent)&&!/AppleWebKit\/5([0-2]|3[0-2])/i.test(e.userAgent)),hashChange:Boolean(!("onhashchange"in a||"onhashchange"in d)||m.isInternetExplorer()&&m.getInternetExplorerMajorVersion()<8)}
    

    破解解决方案:

    m.emulated={pushState:true,hashChange:true}
    

    我正在使用 HTML4+HTML5 捆绑的缩小代码,但该行对应于 history.js 未压缩文件中的第 269 行。如果您使用的是不同的版本,相应的部分在这里:

    未缩小的原件 (View on GitHub):

    History.emulated = {
    pushState: !Boolean(
    window.history && window.history.pushState && window.history.replaceState
    && !(
    (/ Mobile\/([1-7][a-z]|(8([abcde]|f(1[0-8]))))/i).test(navigator.userAgent) /* disable for versions of iOS before version 4.3 (8F190) */
    || (/AppleWebKit\/5([0-2]|3[0-2])/i).test(navigator.userAgent) /* disable for the mercury iOS browser, or at least older versions of the webkit engine */
    )
    ),
    hashChange: Boolean(
    !(('onhashchange' in window) || ('onhashchange' in document))
    ||
    (History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 8)
    )
    };
    

    破解解决方案:

    History.emulated = {
        pushState: true,
        hashChange: true
        };
    

    【讨论】:

    • 他们为什么不把这个设为默认值?似乎每个人都希望# 版本工作?
    • @JamieHutber 当浏览器降级时,您确实会回退到哈希 - 这些代码更改是为了在您通常使用推送状态的 HTML5 浏览器上强制哈希版本。
    • 提醒新读者,history.js 有一个强制使用主题标签的选项 - 来自 history.js 文档:History.options.html4Mode - 如果为真,将强制 HTMl4 模式(主题标签)跨度>
    • 任何人都在插件(或 GreaseMonkey 脚本)中得到了这个,所以它对所有站点都被禁用。我厌倦了执行不佳的应用程序,比如谷歌地图,仅仅因为我移动了一点地图,就用数百个条目填充了我的历史记录,或者具有滚动功能的网站,当你滚动时会创建历史条目,但从来没有真正重新加载你来时所拥有的内容回到他们身边。
    猜你喜欢
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 2011-06-30
    • 2015-01-19
    • 1970-01-01
    • 2014-03-03
    • 2012-05-28
    • 2012-07-28
    相关资源
    最近更新 更多