开发者

How to tell if a JavaScript language feature is supported by a given browser

O开发者_JAVA百科ften 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?


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:

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜