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?

k13i
  • 4,011
  • 3
  • 35
  • 63

1 Answers1

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

Community
  • 1
  • 1
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60