Android JavaScript 如何检查移动/平板电脑的用户代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21757105/
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
JavaScript how to check User Agent for Mobile/Tablet
提问by Myles
I'm currently developing some JS work for a clients website which has different functionality across desktop and tablet platforms. Consider:
我目前正在为客户网站开发一些 JS 工作,该网站在桌面和平板电脑平台上具有不同的功能。考虑:
if(! navigator.userAgent.match(/Android/i) &&
! navigator.userAgent.match(/webOS/i) &&
! navigator.userAgent.match(/iPhone/i) &&
! navigator.userAgent.match(/iPod/i) &&
! navigator.userAgent.match(/iPad/i) &&
! navigator.userAgent.match(/Blackberry/i) )
{
// do desktop stuff
} else if ( navigator.userAgent.match(/iPad/i) )
{
// do tablet stuff
}
Currently, I'm only checking for iPad as checking for "android" seems somewhat problematic, and is a very broad term. Is there a known method for distinguishing between Android tablet & mobile using JS?
目前,我只检查 iPad,因为检查“android”似乎有些问题,而且是一个非常宽泛的术语。是否有使用 JS 区分 Android 平板电脑和手机的已知方法?
Many thanks, Myles
非常感谢,迈尔斯
回答by oriadam
First - improving the condition
第一 - 改善病情
if (navigator.userAgent.match(/iPad/i))
{
// do tablet stuff
} else if(navigator.userAgent.match(/Android|webOS|iPhone|iPod|Blackberry/i) )
{
// do mobile stuff
} else {
// do desktop stuff
}
Second, adding the missing keywords:
二、添加缺失的关键字:
if (navigator.userAgent.match(/Tablet|iPad/i))
{
// do tablet stuff
} else if(navigator.userAgent.match(/Mobile|Windows Phone|Lumia|Android|webOS|iPhone|iPod|Blackberry|PlayBook|BB10|Opera Mini|\bCrMo\/|Opera Mobi/i) )
{
// do mobile stuff
} else {
// do desktop stuff
}
And third, this is the actual detection script that I use:
第三,这是我使用的实际检测脚本:
https://jsfiddle.net/oriadam/ncb4n882/
https://jsfiddle.net/oriadam/ncb4n882/
var
ua = navigator.userAgent,
browser = /Edge\/\d+/.test(ua) ? 'ed' : /MSIE 9/.test(ua) ? 'ie9' : /MSIE 10/.test(ua) ? 'ie10' : /MSIE 11/.test(ua) ? 'ie11' : /MSIE\s\d/.test(ua) ? 'ie?' : /rv\:11/.test(ua) ? 'ie11' : /Firefox\W\d/.test(ua) ? 'ff' : /Chrome\W\d/.test(ua) ? 'gc' : /Chromium\W\d/.test(ua) ? 'oc' : /\bSafari\W\d/.test(ua) ? 'sa' : /\bOpera\W\d/.test(ua) ? 'op' : /\bOPR\W\d/i.test(ua) ? 'op' : typeof MSPointerEvent !== 'undefined' ? 'ie?' : '',
os = /Windows NT 10/.test(ua) ? "win10" : /Windows NT 6\.0/.test(ua) ? "winvista" : /Windows NT 6\.1/.test(ua) ? "win7" : /Windows NT 6\.\d/.test(ua) ? "win8" : /Windows NT 5\.1/.test(ua) ? "winxp" : /Windows NT [1-5]\./.test(ua) ? "winnt" : /Mac/.test(ua) ? "mac" : /Linux/.test(ua) ? "linux" : /X11/.test(ua) ? "nix" : "",
mobile = /IEMobile|Windows Phone|Lumia/i.test(ua) ? 'w' : /iPhone|iP[oa]d/.test(ua) ? 'i' : /Android/.test(ua) ? 'a' : /BlackBerry|PlayBook|BB10/.test(ua) ? 'b' : /Mobile Safari/.test(ua) ? 's' : /webOS|Mobile|Tablet|Opera Mini|\bCrMo\/|Opera Mobi/i.test(ua) ? 1 : 0,
tablet = /Tablet|iPad/i.test(ua),
touch = 'ontouchstart' in document.documentElement;
EDIT:
编辑:
Note that tablets and iPads are considered both mobile
and tablet
.
Note that there are laptops with both touch, mouse and keyboard. In other words a good programmer does not depend on touch
- instead it support all three input methods in all cases.
请注意,平板电脑和 iPad 均被视为mobile
和tablet
。请注意,有触摸、鼠标和键盘的笔记本电脑。换句话说,一个好的程序员不依赖于touch
——而是在所有情况下都支持所有三种输入法。
回答by SHUBHASIS MAHATA
you could use follow those steps
你可以按照这些步骤使用
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()) {
window.location = "#";
}