Javascript 如何查找浏览器是否支持 History.Pushstate?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6824991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:20:20  来源:igfitidea点击:

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

javascriptcross-browserhashpushstate

提问by Ashish Agarwal

I want to change URL without without reloading the page. The possible solution I found is

我想在不重新加载页面的情况下更改 URL。我发现的可能解决方案是

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

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

but some browser like Firefox 3.5, IE6+ does not support this, so for them solution is

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

var uri = window.location.href;

var uri = window.location.href;

but the issue is how to discover if a browser supports history.pushstate or not?

但问题是如何发现浏览器是否支持 history.pushstate ?

Is TRY CATCH is the possible solution or any thing else.

TRY CATCH 是可能的解决方案还是其他任何事情。

回答by Kon

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

Quickest test is to run this in the browser console to see if it's supported:

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

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

Also notice that in FF typeof(history.pushState)returns "function", while in IE it returns "undefined"

另请注意,在 FF 中typeof(history.pushState)返回“函数”,而在 IE 中返回“未定义”