通过Javascript / jQuery检测Android手机。

时间:2021-01-03 21:20:17

How would i detect that the device in use is an Android for a mobile website?

我如何检测正在使用的设备是移动网站的Android ?

I need to apply certain css attributes to the Android platform.

我需要在Android平台上应用一些css属性。

Thanks

谢谢

6 个解决方案

#1


179  

Take a look at that : http://davidwalsh.name/detect-android

看看这个:http://davidwalsh.name/dete-android

JavaScript:

JavaScript:

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
  // Do something!
  // Redirect to Android-site?
  window.location = 'http://android.davidwalsh.name';
}

PHP:

PHP:

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($ua,'android') !== false) { // && stripos($ua,'mobile') !== false) {
  header('Location: http://android.davidwalsh.name');
  exit();
}

Edit : As pointed out in some comments, this will work in 99% of the cases, but some edge cases are not covered. If you need a much more advanced and bulletproofed solution in JS, you should use platform.js : https://github.com/bestiejs/platform.js

编辑:正如一些评论所指出的,这将在99%的情况下起作用,但是一些边缘情况没有被涵盖。如果需要在JS中使用更高级的、防弹的解决方案,应该使用platform。js:https://github.com/bestiejs/platform.js

#2


34  

How about this one-liner?

这一行程序怎么样?

var isAndroid = /(android)/i.test(navigator.userAgent);

The i modifier is used to perform case-insensitive matching.

i修饰符用于执行不区分大小写的匹配。


Technique taken from Cordova AdMob test project: https://github.com/floatinghotpot/cordova-admob-pro/wiki/00.-How-To-Use-with-PhoneGap-Build

技术取自Cordova AdMob测试项目:https://github.com/floatinghotpot/cordova- AdMob - pro/wiki/00.-how - to - use - phonegap - build

#3


4  

No need for jQuery here. As already mentioned before, you should check it using the user agent property. If you're interested in detecting other platforms as well, or just whether you're on a mobile device in general, I wrote this is-mobile component that can detect it.

这里不需要jQuery。如前所述,您应该使用用户代理属性检查它。如果你也对检测其他平台感兴趣,或者仅仅是你是否在移动设备上,我写了一个可以检测它的is-mobile组件。

Feel free to import it and use as shown in these examples:

如下列例子所示,请随意导入和使用:

isMobile.Android() => true/false
isMobile.iOS() => true/false
isMobile.Windows() => true/false
isMobile.KindleFire() => true/false
isMobile.any() => true/false

#4


2  

;(function() {
    var redirect = false
    if (navigator.userAgent.match(/iPhone/i)) {
        redirect = true
    }
    if (navigator.userAgent.match(/iPod/i)) {
        redirect = true
    }
    var isAndroid = /(android)/i.test(navigator.userAgent)
    var isMobile = /(mobile)/i.test(navigator.userAgent)
    if (isAndroid && isMobile) {
        redirect = true
    }
    if (redirect) {
        window.location.replace('jQueryMobileSite')
    }
})()

#5


1  

I think Michal's answer is the best, but we can take it a step further and dynamically load an Android CSS as per the original question:

我认为Michal的回答是最好的,但是我们可以更进一步,根据最初的问题动态加载一个Android CSS:

var isAndroid = /(android)/i.test(navigator.userAgent);
if (isAndroid) {
    var css = document.createElement("link");
    css.setAttribute("rel", "stylesheet");
    css.setAttribute("type", "text/css");
    css.setAttribute("href", "/css/android.css");
    document.body.appendChild(css);
}

#6


-2  

js version, catches iPad too:

js版,也抓iPad:

var is_mobile = /mobile|android/i.test (navigator.userAgent);

#1


179  

Take a look at that : http://davidwalsh.name/detect-android

看看这个:http://davidwalsh.name/dete-android

JavaScript:

JavaScript:

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
  // Do something!
  // Redirect to Android-site?
  window.location = 'http://android.davidwalsh.name';
}

PHP:

PHP:

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($ua,'android') !== false) { // && stripos($ua,'mobile') !== false) {
  header('Location: http://android.davidwalsh.name');
  exit();
}

Edit : As pointed out in some comments, this will work in 99% of the cases, but some edge cases are not covered. If you need a much more advanced and bulletproofed solution in JS, you should use platform.js : https://github.com/bestiejs/platform.js

编辑:正如一些评论所指出的,这将在99%的情况下起作用,但是一些边缘情况没有被涵盖。如果需要在JS中使用更高级的、防弹的解决方案,应该使用platform。js:https://github.com/bestiejs/platform.js

#2


34  

How about this one-liner?

这一行程序怎么样?

var isAndroid = /(android)/i.test(navigator.userAgent);

The i modifier is used to perform case-insensitive matching.

i修饰符用于执行不区分大小写的匹配。


Technique taken from Cordova AdMob test project: https://github.com/floatinghotpot/cordova-admob-pro/wiki/00.-How-To-Use-with-PhoneGap-Build

技术取自Cordova AdMob测试项目:https://github.com/floatinghotpot/cordova- AdMob - pro/wiki/00.-how - to - use - phonegap - build

#3


4  

No need for jQuery here. As already mentioned before, you should check it using the user agent property. If you're interested in detecting other platforms as well, or just whether you're on a mobile device in general, I wrote this is-mobile component that can detect it.

这里不需要jQuery。如前所述,您应该使用用户代理属性检查它。如果你也对检测其他平台感兴趣,或者仅仅是你是否在移动设备上,我写了一个可以检测它的is-mobile组件。

Feel free to import it and use as shown in these examples:

如下列例子所示,请随意导入和使用:

isMobile.Android() => true/false
isMobile.iOS() => true/false
isMobile.Windows() => true/false
isMobile.KindleFire() => true/false
isMobile.any() => true/false

#4


2  

;(function() {
    var redirect = false
    if (navigator.userAgent.match(/iPhone/i)) {
        redirect = true
    }
    if (navigator.userAgent.match(/iPod/i)) {
        redirect = true
    }
    var isAndroid = /(android)/i.test(navigator.userAgent)
    var isMobile = /(mobile)/i.test(navigator.userAgent)
    if (isAndroid && isMobile) {
        redirect = true
    }
    if (redirect) {
        window.location.replace('jQueryMobileSite')
    }
})()

#5


1  

I think Michal's answer is the best, but we can take it a step further and dynamically load an Android CSS as per the original question:

我认为Michal的回答是最好的,但是我们可以更进一步,根据最初的问题动态加载一个Android CSS:

var isAndroid = /(android)/i.test(navigator.userAgent);
if (isAndroid) {
    var css = document.createElement("link");
    css.setAttribute("rel", "stylesheet");
    css.setAttribute("type", "text/css");
    css.setAttribute("href", "/css/android.css");
    document.body.appendChild(css);
}

#6


-2  

js version, catches iPad too:

js版,也抓iPad:

var is_mobile = /mobile|android/i.test (navigator.userAgent);