使用Javascript进行Opera Mini浏览器检测 [英] Opera Mini Browser Detection Using Javascript

查看:183
本文介绍了使用Javascript进行Opera Mini浏览器检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的网站编写了一个javascript代码,用于检测它是否在移动设备上的Opera Mini浏览器上运行。由于Opera Mini具有数据保存功能,因此启用它时有时无法正确加载站点,因此我想通过检测所使用的浏览器是否为Opera Mini来显示消息。

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.

下面发布的代码适用于iOS上的Opera Mini,但它不适用于Android。有任何建议使代码也适用于Android上的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>

推荐答案

之前从未听说过Opera Mini,但我确实谷歌了,发现 https://dev.opera.com/articles/opera-mini-and-javascript/ 。基本上,她正在使用用户代理字符串来确定它是否是歌剧迷你

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.

但是,她还建议您可以使用window对象来确定这一点。 Opera Mini还包含一个operamini对象作为窗口对象的属性。要检查是否存在此对象,请使用以下代码。

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]"

如果你仍然无法让它工作,我会提出另一种方法。通过尝试使用保存功能进行保存来解决此问题,如果失败,请使用您打算使用的任何后备保存功能。因此,只要它是opera mini,它就能够使用数据保存功能,但是当它不是opera mini时,它将使用替代功能。想想这里的尝试/捕获。当然,您也可以在检索保存的数据时考虑实现。

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.

这篇关于使用Javascript进行Opera Mini浏览器检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆