Javascript 检测桌面浏览器与移动浏览器的可靠方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14440296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:46:09  来源:igfitidea点击:

Reliable Way to Detect Desktop vs. Mobile Browser

phpjavascriptcross-browserbrowser-detection

提问by moey

Possible Duplicate:
What is the Best way to do Browser Detection in Javascript?

可能的重复:
在 Javascript 中进行浏览器检测的最佳方法是什么?

I'd like to essentially do the following (in JavaScript or PHP):

我想基本上执行以下操作(在 JavaScript 或 PHP 中):

if (desktop browser) {
     do x;
}
else {   // mobile browser
     do not do x;
}

It is known that using a browser detection method is not recommended. A better solution is using a capability testing. My question is, with mobile browsers become smarter and as powerful as the desktop version, what ideally exclusive capability detection to filter the desktop from non-desktop browsers?

众所周知,不推荐使用浏览器检测方法。更好的解决方案是使用能力测试。我的问题是,随着移动浏览器变得与桌面版本一样智能和强大,理想情况下,从非桌面浏览器中过滤桌面的唯一功能检测是什么?

I think reversing the conditional check i.e. if (mobile browser) {} else ...might prove to be more problematic, right?

我认为反转条件检查 ieif (mobile browser) {} else ...可能会被证明更有问题,对吧?

回答by Rémi Breton

What a little Google search turned up, from A Beautiful Site:

一个小小的谷歌搜索出现了,来自一个美丽的网站

var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

if(isMobile.any()){
    // Mobile!
} else {
    // Not mobile
}

I will not argue that feature detection is waypreferable to user-agent sniffing, which is terrible actually. But if you're detecting feature to determine whether or not the device is considered mobile or not, you're exposing yourself to a whole new serie of problems.

我不会说这个功能检测方法最好用户代理嗅探,这是可怕的实际。但是,如果您正在检测功能以确定设备是否被视为移动设备,那么您将面临一系列全新的问题。

You can't check pixel-ratiobecause new desktops computers will most likely be "retina" or super-HD. You can't check device-orientationbecause it's not something unique to mobiles anymore. You can't check (if you can) the gyroscope because some laptop might return values.

您无法检查,pixel-ratio因为新的台式计算机很可能是“视网膜”或超高清。您无法检查,device-orientation因为它不再是手机独有的东西。您无法检查(如果可以)陀螺仪,因为某些笔记本电脑可能会返回值。

Build websites that works on all platforms without trying to separate them!

构建适用于所有平台的网站,而无需尝试将它们分开!

回答by Vlad Preda

I think you're looking for the get_browser()function in php.

我认为您正在php 中寻找get_browser()函数。

Attempts to determine the capabilities of the user's browser, by looking up the browser's information in the browscap.ini file.

尝试通过在 browscap.ini 文件中查找浏览器信息来确定用户浏览器的功能。