Question

This question is quite simple, but I want to ask it anyway.

This code tell us that user's browser is a famous Opera Mini

var isOperaMini = (navigator.userAgent.indexOf('Opera Mini') > -1);

So I can use it in this way:

if (navigator.userAgent.indexOf('Opera Mini') > -1)
    alert('hey! your browser is buggy');

This is if Opera Mini, then conditional.

How do I make a right short conditional for if not Opera mini, then?

I'm not sure how should I play with -1 integer.

Was it helpful?

Solution

You can simply change it to

if (navigator.userAgent.indexOf('Opera Mini') == -1)

That will return true if it does NOT find 'Opera Mini' in the user agent string

OTHER TIPS

From the Opera doc:

Detecting Opera Mini: 2 approaches: You can:

  1. Examine the user agent string

  2. Check for the presence of the operamini object

i.e. If not Opera Mini is:

if (!window.operamini){}

Short, easy and reliable. Unlike the spoof-able user-agent.

Another options (maybe less readable) using ~ operator:

if (~navigator.userAgent.indexOf('Opera Mini')) {
    // opera mini
}

if (!~navigator.userAgent.indexOf('Opera Mini')) {
    // not opera mini
}

However it's probably better for you to compare with -1, until you are familiar with syntax.

https://dev.opera.com/articles/opera-mini-and-javascript/

var isOperaMini = Object.prototype.toString.call(window.operamini) === "[object OperaMini]"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top