是否有人知道一个库可以确定是否可以使用pushState?
我在用这个:
if(window.history.pushState){
window.history.pushState(null, document.title, path);
}else{
location.pathname = path;
}但是我刚刚发现Safari5.0.2中有一个bug,即使上面的测试通过了:http://support.github.com/discussions/site/2263-line-links-broken,它还是不能工作。
我想可能还有其他的问题,可能有人已经找到并包装好了,但我还没有找到任何东西。
编辑:@新月
据我所见,pushState似乎推动了历史堆栈并更改了url,但没有更新location.pathname。在我的代码中,我使用setInterval检查路径是否已更新。
var cachedPathname = location.pathname;
if(window.history.pushState){
cachedPathname = location.pathname;
setInterval(function(){
if(cachedPathname !== location.pathname){
cachedPathname = location.pathname;
//do stuff
}
}, 100);
}在Safari5.0.2中,当location.pathname更改url时,pushState不会改变。这适用于其他浏览器和Safari版本。
发布于 2012-05-18 05:56:54
看看现代的源代码,这就是它如何检查推送状态:
tests['history'] = function() {
return !!(window.history && history.pushState);
};所以对你来说,一个简单的方法就是:
var hasPushstate = !!(window.history && history.pushState);在深入两个层次之前,必须首先检查window.history的存在,这可能就是您遇到错误的原因。
发布于 2011-11-01 05:23:01
pushState是HTML5历史API的一部分。您可以使用常规JavaScript进行测试以获得支持,如下所示:
if (typeof history.pushState !== "undefined") {
// pushState is supported!
}或者,您可以使用现代派库:
if (Modernizr.history) {
// pushState is supported!
}https://stackoverflow.com/questions/4966090
复制相似问题