我的目标是仅支持HTML5浏览器的AJAX历史记录。但是,我希望我的网站能够使用HTML4浏览器,但是没有AJAX历史记录。HTML5的History.js - Hack不需要打破IE7
许多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和历史.js插件需要JSON.parse
和JSON.stringify
。
建议的解决方案是包含json2.js(link)。这对我来说似乎很奇怪,因为支持pushState()
和popState()
的HTML5浏览器也应该支持本机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 };
}
这似乎工作,但感觉像一个破解。有一个更好的方法吗?
编辑:7/31/2012
如果我不包括history.html4.js它仍然使我对IE7的错误。看来,包括json2.js只是此时插件的一个要求。如果没有,可以改进静默检查JSON支持并禁用该插件,但现在我有一个解决方法。下面是一个History.js SNIPPIT:
/**
* 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只会导致错误。
如果您不包含history.html4,会发生什么情况?从源头上看,它看起来像只是默默不会初始化... – 2012-07-31 06:41:52