HTML5 的 History.js - 需要进行黑客攻击才能不破坏 IE7

2023-12-01

我的目标是仅支持 HTML5 浏览器的 AJAX 历史记录。但是,我希望我的网站能够使用 HTML4 浏览器,但没有 AJAX 历史记录。

许多 History.js 示例在执行任何操作之前都包含以下检查:

if (!History.enabled) {
    // History.js is disabled for this browser.
    // This is because we can optionally choose to support HTML4 browsers or not.
    return false;
}

这似乎可行,除了 IE7 等旧版浏览器不支持本机 JSON 并且 History.js 插件需要JSON.parse and JSON.stringify.

建议的解决方案是包含 json2.js (link)。这对我来说有点奇怪,因为 HTML5 浏览器支持pushState() and popState()还应该支持原生 JSON。另外,我不想包含另一个我并不真正需要的库。我的解决方案是有条件地包含 History.js,如下所示:

var nativeJSON = (typeof JSON === 'object') && (typeof JSON.parse === 'function') && (typeof JSON.stringify === 'function');
if (nativeJSON) {
    /// Include contents of: balupton-history.js-e84ad00\scripts\bundled\html5\jquery.history.js
} else {
    window.History = { enabled: false };
}

这似乎有效,但感觉就像黑客。有一个更好的方法吗?

编辑:2012 年 7 月 31 日

如果我不包含history.html4.js,它在 IE7 上仍然会出现错误。目前看来,包含 json2.js 只是该插件的一个要求。可能可以进行改进,以静默检查 JSON 支持并禁用该插件(如果没有),但现在我有一个解决方法。这是来自 History.js 的片段:

/**
 * History.js Core
 * @author Benjamin Arthur Lupton <[email protected]>
 * @copyright 2010-2011 Benjamin Arthur Lupton <[email protected]>
 * @license New BSD License <http://creativecommons.org/licenses/BSD/>
 */

(function(window,undefined){
    "use strict";

    // ========================================================================
    // Initialise

    // Localise Globals
    var
        console = window.console||undefined, // Prevent a JSLint complain
        document = window.document, // Make sure we are using the correct document
        navigator = window.navigator, // Make sure we are using the correct navigator
        sessionStorage = window.sessionStorage||false, // sessionStorage
        setTimeout = window.setTimeout,
        clearTimeout = window.clearTimeout,
        setInterval = window.setInterval,
        clearInterval = window.clearInterval,
        JSON = window.JSON,
        alert = window.alert,
        History = window.History = window.History||{}, // Public History Object
        history = window.history; // Old History Object

    // MooTools Compatibility
    JSON.stringify = JSON.stringify||JSON.encode;
    JSON.parse = JSON.parse||JSON.decode;

如果 window.JSON 未定义,引用 window.JSON.stringify 只会导致错误。


以下内容在 IE7 中适用于我,没有错误:

<html>
<head>
    <title>Testing</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
        // Tell us whether History is enabled
        var alertHistory = function() {
            alert(History.enabled ? 'enabled' : 'disabled');
        }

        var nativeJSON = (typeof JSON === 'object') && (typeof JSON.parse === 'function') && (typeof JSON.stringify === 'function');
        if (nativeJSON) {
            // Native JSON is present, add History.js
            var historyJs = document.createElement('script');
            historyJs.type = 'text/javascript';
            historyJs.src = 'https://raw.github.com/browserstate/history.js/master/scripts/bundled/html5/jquery.history.js';
            historyJs.onload = alertHistory;
            document.getElementsByTagName("head")[0].appendChild(historyJs);
        } else {
            window.History = { enabled: false };
            alertHistory();
        }
    </script>
</head>
<body>

</body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HTML5 的 History.js - 需要进行黑客攻击才能不破坏 IE7 的相关文章

  • 无法使用 PHP openssl_sign 签署任何内容?

    我正在尝试使用 PHP DKIM 发送 DKIM 签名的电子邮件 它有点旧 所以我不得不改变一些东西 但这让我很难过 Warning openssl sign function openssl sign supplied key param
  • 需要帮助理解该函数中的 movzbl 调用

    所以我尝试通过查看此处的程序集来编写一些 C 代码 pushl ebp movl esp ebp movl 12 ebp eax addl 8 ebp eax movzbl eax eax movsbl al eax popl ebp re
  • PHP float/double 存储为 MySQL DECIMAL

    我在 MySQL 中存储值时遇到了一个非常奇怪的问题 前提 我有一个使用的表DECIMAL 15 8 存储货币值 如订单总额 但是当我尝试插入时 2 45545345 这存储为 2 00000000 我尝试了 MySQL 的 FORMAT
  • 惰性正则表达式的性能和实践?

    I 在互联网上阅读我应该避免使用懒惰的正则表达式 因为 更差 的性能和 糟糕的 实践 我从未见过这两者的例子 我还没有听说过哪个应用程序受正则表达式的 CPU 限制 其他人说他们 学会 避免它 但从未提及原因 而有人暗示这是因为在旧程序中打
  • 如何使用 Android 在 Firestore 中添加时间戳?

    我正在尝试使用 Firebase Firestore 在 Android 客户端中添加时间戳字段 根据文档 用于标记要使用服务器填充的日期字段的注释 时间戳 如果正在写入的 POJO 包含 null ServerTimestamp注释的字段
  • 如何使用expressjs连接?

    var express require express routes require routes http require http path require path fs require fs var app express app

随机推荐