Safari和Chrome(以及,我猜,所有Webkit浏览器)不响应window.history.state
,这是在不断发展的HTML5标准中指定的,并在Firefox中实现。
是否有一个工作区来读取它?也许在代码中触发popstate事件并从事件处理程序返回状态是可能的?
我昨天遇到了这个问题。我不需要一个跨浏览器的解决方案,因为目标平台运行的是一个旧版本的WebKit,所以我在WebKit中为history.state
创建了一个快速而简单的polyfill:
// Polyfill for history.state in older webkit engines
if (!history.hasOwnProperty('state')) {
(function (push, rep) {
// history.state is always initialised to null
history.state = null;
history.pushState = function (state) {
push.apply(history, arguments);
history.state = state;
};
history.replaceState = function (state) {
rep.apply(history, arguments);
history.state = state;
};
window.addEventListener('popstate', function (e) {
history.state = e.state;
}, true);
})(history.pushState, history.replaceState);
}
window.history.state
目前未在webkit浏览器中实现。有一个请求,但是到目前为止还没有实现的步骤。
有一个名为history.js的项目,旨在提供交叉兼容的历史管理体验。
History.js在所有浏览器中优雅地支持HTML5历史/状态api (pushState, replaceState, onPopState)。包括对数据、标题、replaceState的持续支持。支持jQuery, MooTools和Prototype。对于HTML5浏览器,这意味着您可以直接修改URL,而无需再使用散列。对于HTML4浏览器,它将恢复使用旧的onhashchange功能。
从这个项目,History.getState()
似乎做什么是被要求的。