如何判断给定浏览器是否支持 JavaScript 语言功能

发布于 2024-10-10 05:05:52 字数 105 浏览 0 评论 0原文

很多时候,当我编写 JavaScript 代码时,我无法摆脱这样的偏执:某些函数或代码行会在这个浏览器中被破坏,或者......

我如何检查我正在编写的代码是否是x 浏览器兼容吗?

Often times when I'm writing my JavaScript code I can't shake the paranoia that some function or line of code is going to break in this browser or that...

How can I check to see if the code I'm writing is x-browser compatible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惜醉颜 2024-10-17 05:05:52

基本功能检测可以通过查询窗口属性来完成,例如window.XMLHttpRequest

在某些情况下,它比仅仅查询属性要复杂得多,因为定义的方法可能彼此不一致。在这种情况下,这实际上取决于场景,但某些时候您可能需要做一些肮脏的事情,例如创建一个偏离屏幕的元素,然后设置某些属性并查看这些更改是否得到反映。

如果您专门给出某些情况的示例,这可能会很有用,因为实际上有数千种事物需要进行功能检测,但并非所有事物都是相同的。

Basic feature detection can be done by querying window properties, eg window.XMLHttpRequest.

In certain cases it becomes a lot more complex than just querying properties, since methods defined can be inconsistent with one another. In that case it really depends on the scenario but certain times you may need to do something as dirty as creating an element that's offset from the screen, then set certain properties and see if those changes reflected or not.

It would probably be useful if you specifically gave examples of certain cases, as there are literally thousands of things to feature detect for and not everything is the same.

留一抹残留的笑 2024-10-17 05:05:52

您可以检查您要使用的功能是否存在,如下所示:

if(functionOrObjectName) {
    //will execute if the function or object is defined
}

You can check if the function for the feature you want to use exists like this:

if(functionOrObjectName) {
    //will execute if the function or object is defined
}
混吃等死 2024-10-17 05:05:52

您可能会发现这个测试/兼容性表网站很方便: http://robertnyman.com/javascript/

也请查看 has.js 。它应该会稍微简化特征检测。

You might find this test/compatibility table site handy: http://robertnyman.com/javascript/ .

Check out has.js too. It should simplify feature detection a bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文