첫째 - 조건
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 }
둘째
을 개선 누락 된 키워드를 추가 :
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
}
그리고 세 번째, 이것은이다 우리의 실제 탐지 스크립트 E :이
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;
편집 : 태블릿과 아이 패드가 mobile
및 tablet
모두 간주됩니다
참고. 터치, 마우스 및 키보드가 모두있는 랩톱이 있습니다. 다른 말로하면 좋은 프로그래머는 touch
에 의존하지 않고 대신 모든 세 가지 입력 방법을 모두 지원합니다.
이 질문에 대한 의견에 따르면 : http://stackoverflow.com/questions/5341637/how-do-detect-android-tablets-in-general-useragent '모바일' UA에서. 이 문제를 해결할 수있는 방법은 없나요? – Myles