11/20/2014

Opera Browser: How to identify an Opera user with Javascript

Every browser has different capabilities. I've been working with the Web Speech API, which is not yet widely supported. Apparently, Opera used to have text-to-speech capabilities, but does not currently (Nov. 2014).

If I test for Web Speech API, Opera says it has the SpeechSynthesisUtterance object available, but it doesn't work, so I had to write code that would weed out Opera browsers.

What you need to do is get the browser to tell you what brand it is. This is done through Javascript with the navigator object and its useragent property. You want to try and find a match for "Opera/" or "OPR/".

And to ensure that the response is a boolean (true/false) value, you will use the double-negative comparison operator: "!!"

Together, this is what it looks like:

  if (!! navigator.userAgent.match(/(Opera|OPR)\//i)) {
    // DO SOMETHING IF IT'S OPERA
  }

The "OPR" identification match is fairly new. It used to be Opera. The trailing slash is used to avoid matching some other arbitrary characters in future browsers.

No comments :

Post a Comment