gpt4 book ai didi

用于 HTML5 的 History.js - 黑客需要不破坏 IE7

转载 作者:行者123 更新时间:2023-12-04 18:16:38 24 4
gpt4 key购买 nike

我的目标是仅支持 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 之外,这似乎可行。和 JSON.stringify .

建议的解决方案是包含 json2.js ( link )。这对我来说似乎有点奇怪,因为支持 pushState() 的 HTML5 浏览器和 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 <contact@balupton.com>
* @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
* @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>

关于用于 HTML5 的 History.js - 黑客需要不破坏 IE7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11549661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com