如何判断给定浏览器是否支持 JavaScript 语言功能
很多时候,当我编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本功能检测可以通过查询窗口属性来完成,例如
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.
您可以检查您要使用的功能是否存在,如下所示:
You can check if the function for the feature you want to use exists like this:
您可能会发现这个测试/兼容性表网站很方便: 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.