Derek Adair
Derek Adair

Reputation: 21935

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

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?

Upvotes: 1

Views: 2668

Answers (3)

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.

Upvotes: 0

meder omuraliev
meder omuraliev

Reputation: 186742

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.

Upvotes: 4

Robber
Robber

Reputation: 419

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
}

Upvotes: 0

Related Questions