检查HTML5 History API支持的浏览器兼容性?

Aru*_*vid 13 javascript browser jquery html5

有没有办法检查浏览器是否支持使用JavaScript的'HTML5 History API'.

我是否必须使用if语句中的长条件列表检查所有浏览器及其版本.或者只是像使用'if'语句检查任何函数对象就足够了......

Jam*_*unt 29

检查pushState()全局历史对象上是否存在方法应该就足够了.

function supports_history_api() {
  return !!(window.history && history.pushState);
}
Run Code Online (Sandbox Code Playgroud)

对于更一般的HTML 5特征检测,我会看看Modernizer

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

这将使您的代码与每个测试的混乱细节隔离开来,使代码更易读,更不容易出错.使用页面上的Modernizer脚本,您只需执行以下操作:

if (Modernizr.history) {
  // history management works!
} else {
  // no history support :(
  // fall back to a scripted solution like History.js
}
Run Code Online (Sandbox Code Playgroud)