1

I found this code snippet, which checks if Safari browser is used:

var isSafari = /constructor/i.test(window.HTMLElement);

but I don't really know what's going on here. Could someone explain me how it works? I know that constructor returns the function that created the RegExp object's prototype and i performs case-insensitive matching, but what exactly is HTMLElement?

0

1 Answer 1

5

In safari, window.HTMLElement returns a function which is named HTMLElementConstructor.

So let do this:

/constructor/i.test(function HTMLElementConstructor() {}) // return true

But with other browsers (FF, Chrome), it returns HTMLElement

/constructor/i.test(function HTMLElement() {}) // return false

But thank you for this observation! I hope we can use it as well as the method mentionned here: Detect Safari browser

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

1 Comment

This is no longer true with iOS 11 (possibly earlier?)

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.