如何查找浏览器是否支持历史记录.Pushstate或不Pushstate

How to find if a browser supports History.Pushstate or not?

本文关键字:Pushstate 支持 历史 记录 是否 或不 浏览器 何查找 查找      更新时间:2023-09-26

我想改变URL而不需要重新加载页面。我找到的可能的解决方案是

window.history。pushState('page2', 'Title', '/page2.php');

但是一些浏览器如Firefox 3.5, IE6+不支持这个,所以他们的解决方案是

var uri = window.location.href;

,但问题是如何发现浏览器是否支持历史记录。Pushstate还是不Pushstate ?

if (history.pushState) {
  // supported.
}

最快的测试是在浏览器控制台中运行它,看看它是否被支持:

if (history.pushState) { alert('supported'); }

还请注意,在FF typeof(history.pushState)中返回"function",而在IE中返回"undefined"