Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opera Mini Browser Detection Using Javascript

I have written a javascript code for my website to detect if its running on an Opera Mini browser on mobile devices. Since Opera Mini has a data saving feature, when it is enabled it sometimes doesn't load the site properly, hence I want to display a message by detecting whether the browser used is Opera Mini.

The code posted below works perfectly for Opera Mini on iOS but it doesn't work on Android. Any suggestions to make the code also work for Opera Mini on Android?

<script type="text/javascript">
 function o(){
   var isMobile = {
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
                      },
                  };
     if( isMobile.Opera() ) alert('If you are using Opera Mini please disable Data Savings Mode to Have a pleasant browsing experience :)');
             }
 window.onload = o;
</script>
like image 1000
Adhip Rebello Avatar asked Mar 12 '23 14:03

Adhip Rebello


1 Answers

Never heard of Opera Mini before, but I did google for it and found https://dev.opera.com/articles/opera-mini-and-javascript/. Basically, she is using the user agent string to determine if it's opera mini by

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

which is very similar to your method.

However, she also suggests that you can use the window object to determine this. "Opera Mini also includes an operamini object as a property of the window object. To check for the presence of this object, use the following code."

var isOperaMini = Object.prototype.toString.call(window.operamini) === "[object OperaMini]"

If you still can't get it to work, I would propose another approach to this. Approach this problem by trying to save using the saving feature, and if it fails, use the whatever fall back saving feature you intend to use. Therefore, whenever it's opera mini, it would be able to use the data saving feature, but when it isn't opera mini, it would use the alternative feature. Think of a try/catch here. Of course, you would want to consider the implementation when retrieving the saved data as well.

like image 55
Jared Drake Avatar answered Mar 16 '23 03:03

Jared Drake