Reputation: 21935
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
Reputation: 26873
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
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
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