2

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.

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks. The bad thing is, that .now() also needs to be checked after this.
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.
Is there any version of any browser/JS, which evaluates b in if(a && b), assuming that a is false?
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.
"If you're using && it must check both conditions." - this statement is incorrect.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.