javascript 在 Webkit 中读取 window.history.state 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8439145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading window.history.state object in Webkit
提问by Trott
Safari and Chrome (and, I'm guessing, all Webkit browsers) do not respond to window.history.state
which is specified in the evolving HTML5 standard hereand is implemented in Firefox.
Safari 和 Chrome(以及我猜所有的 Webkit 浏览器)不响应此处window.history.state
不断发展的 HTML5 标准中指定的 并在 Firefox 中实现的。
Is there a workaround to read it? Perhaps it is possible to trigger a popstate event in code and return the state from the event handler?
有没有解决方法来阅读它?也许可以在代码中触发 popstate 事件并从事件处理程序返回状态?
采纳答案by kubetz
window.history.state
is not implemented in webkit broswers currently. There is a requestfor it, but no steps towards implementation were done so far.
window.history.state
目前未在 webkit 浏览器中实现。有一个请求,但到目前为止还没有执行任何步骤。
There is a project called history.jswhich is aiming to provide cross-compatible history management experience.
有一个名为history.js的项目旨在提供交叉兼容的历史管理体验。
History.js gracefully supports the HTML5 History/State APIs (pushState, replaceState, onPopState) in all browsers. Including continued support for data, titles, replaceState. Supports jQuery, MooTools and Prototype. For HTML5 browsers this means that you can modify the URL directly, without needing to use hashes anymore. For HTML4 browsers it will revert back to using the old onhashchange functionality.
History.js 优雅地支持所有浏览器中的 HTML5 历史/状态 API(pushState、replaceState、onPopState)。包括对数据、标题、replaceState 的持续支持。支持 jQuery、MooTools 和 Prototype。对于 HTML5 浏览器,这意味着您可以直接修改 URL,而不再需要使用哈希。对于 HTML4 浏览器,它将恢复使用旧的 onhashchange 功能。
From that project, History.getState()
seems to do what is requested.
从那个项目来看,History.getState()
似乎按照要求做。
回答by Andy E
I ran into this issue yesterday. I didn't need a cross browser solution since the target platform is running an oldish version of WebKit, so I created a quick and simple polyfill for history.state
in WebKit:
我昨天遇到了这个问题。我不需要跨浏览器解决方案,因为目标平台运行的是旧版本的 WebKit,所以我在 WebKit 中创建了一个快速简单的 polyfill history.state
:
// 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);
}