I would like to check whether I can use an object/function, for example window.performance.now(). if(typeof window.performance.now != 'undefined') works fine, except for Safari (5.1.7, Windows), which returns TypeError: 'undefined' is not an object (evaluating 'window.performance.now'). To avoid confusion: console.log(typeof window.performance.now) returns the same error.
Add a comment
|
1 Answer
Because the window.performance object itself is not supported in Safari. So you'd be safer checking for
typeof window.performance !== 'undefined' && typeof window.performance.now !== 'undefined'
Update
The first check is to see if the window.performance object exists. The second check is to see if the .now() function is available in the window.performance object.
https://developer.mozilla.org/en-US/docs/Web/API/Performance
10 Comments
john walker
Thanks. The bad thing is, that
.now() also needs to be checked after this.Suvi Vignarajah
Yes, it'd be safe to check for both the availability of the object and the availability of the function. The only cases where you'll get the availability of the object and no support for the
now() function is for IE9 and Opera 15 though - according to the link I provided.john walker
Is there any version of any browser/JS, which evaluates
b in if(a && b), assuming that a is false?Suvi Vignarajah
If you're using
&& it must check both conditions. Edit If you use || it'll only check b if a is not satisfied. So the OR operand would still not work if performance is not available.john walker
"If you're using && it must check both conditions." - this statement is incorrect.
|