kee*_*n3d 11 javascript pushstate
有谁知道一个库确定是否可以使用pushState?
我在用这个:
if(window.history.pushState){
window.history.pushState(null, document.title, path);
}else{
location.pathname = path;
}
Run Code Online (Sandbox Code Playgroud)
但我刚刚发现Safari 5.0.2中存在一个错误,即使上述测试通过,也会导致它无法正常工作:http://support.github.com/discussions/site/2263-line-links-broken.
我想可能还有其他陷阱,有人可能已经发现它们并将它们包裹起来但我还没有找到任何东西.
编辑: @Crescent Fresh
从我所看到的,似乎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);
}
Run Code Online (Sandbox Code Playgroud)
在Safari 5.0.2中,当pushState更改url时,location.pathname不会更改.这适用于其他浏览器和Safari版本.
Mau*_*ord 24
查看Modernizer源代码,这是它检查推送状态的方式:
tests['history'] = function() {
return !!(window.history && history.pushState);
};
Run Code Online (Sandbox Code Playgroud)
所以一个简单的方法就是:
var hasPushstate = !!(window.history && history.pushState);
Run Code Online (Sandbox Code Playgroud)
必须首先检查是否存在window.history前两级深度,这可能是您遇到错误的原因.
dba*_*bau 16
pushState是HTML5 History API的一部分.您可以使用常规JavaScript测试支持,如下所示:
if (typeof history.pushState !== "undefined") {
// pushState is supported!
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用Modernizr库:
if (Modernizr.history) {
// pushState is supported!
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9979 次 |
| 最近记录: |