# Navigator 对象:navigator 对象包含有关浏览器的所有信息,且支持所有浏览器。
navigator 对象有一个属性为 userAgent,这是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值。所以我们可以通过判断 navigator.userAgent
里面是否包含某些值来判断。
# 推荐const detectDeviceType = ( ) => / Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini / i . test ( navigator. userAgent) ? 'Mobile' : 'Desktop' ; detectDeviceType ( ) ;
# 方法一:function IsPC ( ) { var userAgentInfo = navigator. userAgent; var Agents = new Array ( "Android" , "iPhone" , "SymbianOS" , "Windows Phone" , "iPad" , "iPod" ) ; var flag = true ; for ( var v = 0 ; v < Agents. length; v++ ) { if ( userAgentInfo. indexOf ( Agents[ v] ) > 0 ) { flag = false ; break ; } } return flag; }
if ( / (iPhone|iPad|iPod|iOS) / i . test ( navigator. userAgent) ) { window. location. href = "iPhone.html" ; } else if ( / (Android) / i . test ( navigator. userAgent) ) { window. location. href = "Android.html" ; } else { window. location. href = "pc.html" ; } ;
# 方法二:var browser= { versions : function ( ) { var u = navigator. userAgent, app = navigator. appVersion; return { trident: u. indexOf ( 'Trident' ) > - 1 , presto: u. indexOf ( 'Presto' ) > - 1 , webKit: u. indexOf ( 'AppleWebKit' ) > - 1 , gecko: u. indexOf ( 'Gecko' ) > - 1 && u. indexOf ( 'KHTML' ) == - 1 , mobile: ! ! u. match ( / AppleWebKit.*Mobile.* / ) , ios: ! ! u. match ( / \(i[^;]+;( U;)? CPU.+Mac OS X / ) , android: u. indexOf ( 'Android' ) > - 1 || u. indexOf ( 'Linux' ) > - 1 , iPhone: u. indexOf ( 'iPhone' ) > - 1 , iPad: u. indexOf ( 'iPad' ) > - 1 , webApp: u. indexOf ( 'Safari' ) == - 1 , weixin: u. indexOf ( 'MicroMessenger' ) > - 1 , qq: u. match ( / \sQQ / i ) == " qq" } ; } ( ) , language: ( navigator. browserLanguage || navigator. language) . toLowerCase ( ) } if ( browser. versions. mobile || browser. versions. ios || browser. versions. android || browser. versions. iPhone || browser. versions. iPad) { window. location = "http://m.abc.com" ; }
# 方法三:var os = function ( ) { var ua = navigator. userAgent, isWindowsPhone = / (?:Windows Phone) / . test ( ua) , isSymbian = / (?:SymbianOS) / . test ( ua) || isWindowsPhone, isAndroid = / (?:Android) / . test ( ua) , isFireFox = / (?:Firefox) / . test ( ua) , isChrome = / (?:Chrome|CriOS) / . test ( ua) , isTablet = / (?:iPad|PlayBook) / . test ( ua) || ( isAndroid && ! / (?:Mobile) / . test ( ua) ) || ( isFireFox && / (?:Tablet) / . test ( ua) ) , isPhone = / (?:iPhone) / . test ( ua) && ! isTablet, isPc = ! isPhone && ! isAndroid && ! isSymbian; return { isTablet: isTablet, isPhone: isPhone, isAndroid : isAndroid, isPc : isPc } ; } ( ) ; if ( os. isAndroid || os. isPhone) { alert ( "-----" ) ; }
# 方法四:if ( / AppleWebKit.*mobile / i . test ( navigator. userAgent) || ( / MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE / . test ( navigator. userAgent) ) ) { if ( window. location. href. indexOf ( "?mobile" ) < 0 ) { try { if ( / Android|webOS|iPhone|iPod|BlackBerry / i . test ( navigator. userAgent) ) { window. location. href= "手机页面" ; } else if ( / iPad / i . test ( navigator. userAgent) ) { window. location. href= "平板页面" ; } else { window. location. href= "其他移动端页面" } } catch ( e) { } } }
# 方法五:< script language= "javascript" > var system = { win : false , mac : false , xll : false } ; var p = navigator. platform; system. win = p. indexOf ( "Win" ) == 0 ; system. mac = p. indexOf ( "Mac" ) == 0 ; system. x11 = ( p == "X11" ) || ( p. indexOf ( "Linux" ) == 0 ) ; if ( system. win|| system. mac|| system. xll) { } else { window. location. href= "http://abc.com" ; } < / script>
# 方法六:< script language= "javascript" > function is_mobile ( ) { var regex_match = / (nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220) / i ; var u = navigator. userAgent; if ( null == u) { return true ; } var result = regex_match. exec ( u) ; if ( null == result) { return false } else { return true } } if ( is_mobile ( ) ) { document. location. href= 'http://domain.com' ; } < / script>
# 方法 7: 百度 webapp 版< ! -- - 识别手机或电脑的js开始-- - > < script language= "javascript" > ( function ( ) { var res = GetRequest ( ) ; var par = res[ 'index' ] ; if ( par!= 'gfan' ) { var ua= navigator. userAgent. toLowerCase ( ) ; var contains = function ( a, b ) { if ( a. indexOf ( b) != - 1 ) { return true ; } } ; var toMobileVertion = function ( ) { window. location. href = 'http://abc.com/' } if ( contains ( ua, "ipad" ) || ( contains ( ua, "rv:1.2.3.4" ) ) || ( contains ( ua, "0.0.0.0" ) ) || ( contains ( ua, "8.0.552.237" ) ) ) { return false } if ( ( contains ( ua, "android" ) && contains ( ua, "mobile" ) ) || ( contains ( ua, "android" ) && contains ( ua, "mozilla" ) ) || ( contains ( ua, "android" ) && contains ( ua, "opera" ) ) || contains ( ua, "ucweb7" ) || contains ( ua, "iphone" ) ) { toMobileVertion ( ) ; } } } ) ( ) ; function GetRequest ( ) { var url = location. search; var theRequest = new Object ( ) ; if ( url. indexOf ( "?" ) != - 1 ) { var str = url. substr ( 1 ) ; strs = str. split ( "&" ) ; for ( var i = 0 ; i < strs. length; i ++ ) { theRequest[ strs[ i] . split ( "=" ) [ 0 ] ] = unescape ( strs[ i] . split ( "=" ) [ 1 ] ) ; } } return theRequest; } < / script> < ! -- - 识别手机或电脑的js结束-- - >