2012-02-25 97 views
11

有没有什么办法来检查浏览器是否支持“HTML5历史API”使用JavaScript。查看HTML5历史API支持的浏览器兼容性?

我一定要检查所有的浏览器及其版本的条件一长串if语句。 或者干脆喜欢用“如果”声明是不够??? ...

+0

检查这个帖子:http://stackoverflow.com/questions/4708358/detect-whether-html5-history-supported-或-不 – mariogl 2012-02-25 17:37:27

+0

[Modernizr的](http://modernizr.com)具有 “历史” 测试。 – Pointy 2012-02-25 17:38:12

+0

参见[CanIUse(http://caniuse.com) – Pointy 2012-02-25 17:39:56

回答

27

检查,为全球历史对象的pushState()方法的存在应该是足够的。

function supports_history_api() { 
    return !!(window.history && history.pushState); 
} 

对于更一般的HTML 5特性检测我想看看近代化

http://diveintohtml5.info/detect.html#modernizr

这将从每个测试的具体凌乱隔离你的代码,使代码更易读,少出错俯卧。随着网页上的近代化脚本,你只是做:

if (Modernizr.history) { 
    // history management works! 
} else { 
    // no history support :(
    // fall back to a scripted solution like History.js 
} 
+0

日Thnx朋友... :) – 2012-02-26 07:00:40

3

检查history.pushStatehistory.replaceState对象的存在应该是足够的,因为它通常与一般的特征检测足够的检查功能的对象。

-1

您可以查看天气或不是一个函数注册:

if (typeof history.pushState != 'undefined') { 
    //Enabled 
} 

然后,只需更换//不管你希望做启用如果它被支持。

0

您可以使用canisuse.js脚本来检测,如果你的浏览器支持的历史或不

caniuse.history()