JS判断pc mobile几种方式

方法一:

  1. function IsPC(){
  2.  
    var userAgentInfo = navigator.userAgent;
  3.  
    var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
  4.  
    var flag = true;
  5.  
    for (var v = 0; v < Agents.length; v++) {
  6.  
    if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }
  7.  
    }
  8.  
    return flag;
  9.  
    }

 

  1.  
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  2.  
    //alert(navigator.userAgent);
  3.  
    window.location.href ="iPhone.html";
  4.  
    } else if (/(Android)/i.test(navigator.userAgent)) {
  5.  
    //alert(navigator.userAgent);
  6.  
    window.location.href ="Android.html";
  7.  
    } else {
  8.  
    window.location.href ="pc.html";
     
    };

方法二: 

  1. var browser={
  2.  
    versions:function(){
  3.  
    var u = navigator.userAgent, app = navigator.appVersion;
  4.  
    return {//移动终端浏览器版本信息
  5.  
    trident: u.indexOf('Trident') > -1, //IE内核
  6.  
    presto: u.indexOf('Presto') > -1, //opera内核
  7.  
    webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  8.  
    gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  9.  
    mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  10.  
    ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  11.  
    android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
  12.  
    iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器
  13.  
    iPad: u.indexOf('iPad') > -1, //是否iPad
  14.  
    webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  15.  
    weixin: u.indexOf('MicroMessenger') > -1, //是否微信
  16.  
    qq: u.match(/\sQQ/i) == " qq" //是否QQ
  17.  
    };
  18.  
    }(),
  19.  
    language:(navigator.browserLanguage || navigator.language).toLowerCase()
  20.  
    }
  21.  
     
  22.  
    if(browser.versions.mobile || browser.versions.ios || browser.versions.android ||
  23.  
    browser.versions.iPhone || browser.versions.iPad){
  24.  
    window.location = "http://m.zhaizhainv.com";
  25.  
    }
     
     
     

方法三:(推荐,摘自QQ)

 

  1. var os = function() {
  2.  
    var ua = navigator.userAgent,
  3.  
    isWindowsPhone = /(?:Windows Phone)/.test(ua),
  4.  
    isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
  5.  
    isAndroid = /(?:Android)/.test(ua),
  6.  
    isFireFox = /(?:Firefox)/.test(ua),
  7.  
    isChrome = /(?:Chrome|CriOS)/.test(ua),
  8.  
    isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
  9.  
    isPhone = /(?:iPhone)/.test(ua) && !isTablet,
  10.  
    isPc = !isPhone && !isAndroid && !isSymbian;
  11.  
    return {
  12.  
    isTablet: isTablet,
  13.  
    isPhone: isPhone,
  14.  
    isAndroid : isAndroid,
  15.  
    isPc : isPc
  16.  
    };
  17.  
    }();
  18.  
    if(os.isAndroid || os.isPhone){
  19.  
        alert("-----");
  20.  

方法四:

 

  1. 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))){
  2.  
    if(window.location.href.indexOf("?mobile")<0){
  3.  
    try{
  4.  
    if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
  5.  
    window.location.href="手机页面";
  6.  
    }else if(/iPad/i.test(navigator.userAgent)){
  7.  
    window.location.href="平板页面";
  8.  
    }else{
  9.  
    window.location.href="其他移动端页面"
  10.  
    }
  11.  
    }catch(e){}
  12.  
    }
  13.  
    }
     

方法五:

 

  1. //平台、设备和操作系统
  2.  
    var system ={
  3.  
    win : false,
  4.  
    mac : false,
  5.  
    xll : false
  6.  
    };
  7.  
    //检测平台
  8.  
    var p = navigator.platform;
  9.  
    system.win = p.indexOf("Win") == 0;
  10.  
    system.mac = p.indexOf("Mac") == 0;
  11.  
    system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
  12.  
    //跳转语句,如果是手机访问就自动跳转到caibaojian.com页面
  13.  
    if(system.win||system.mac||system.xll){
  14.  
    }else{
  15.  
    window.location.href="http://caibaojian.com";
  16.  
    }

方法六:

 

  1. function is_mobile() {
  2.  
    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;
  3.  
    var u = navigator.userAgent;
  4.  
    if (null == u) {
  5.  
    return true;
  6.  
    }
  7.  
    var result = regex_match.exec(u);
  8.  
     
  9.  
    if (null == result) {
  10.  
    return false
  11.  
    } else {
  12.  
    return true
  13.  
    }
  14.  
    }
  15.  
    if (is_mobile()) {
  16.  
    document.location.href= 'http://caibaojian.com'; //修改http://caibaojian.com为你所需跳转目标页地址
  17.  
    }

方法七:(推荐,百度wepapp)

 

  1. <!---识别手机或电脑的js开始--->
  2.  
    <script language="javascript">
  3.  
    (function(){
  4.  
    var res = GetRequest();
  5.  
    var par = res['index'];
  6.  
    if(par!='gfan'){
  7.  
    var ua=navigator.userAgent.toLowerCase();
  8.  
    var contains=function (a, b){
  9.  
    if(a.indexOf(b)!=-1){return true;}
  10.  
    };
  11.  
    //将下面的http://caibaojian.com改成你的wap手机版页面地址 如我的 http://caibaojian.com
  12.  
    var toMobileVertion = function(){
  13.  
    window.location.href = 'http://caibaojian.com/'
  14.  
    }
  15.  
     
  16.  
    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}
  17.  
    if((contains(ua,"android") && contains(ua,"mobile"))||(contains(ua,"android") && contains(ua,"mozilla")) ||(contains(ua,"android") && contains(ua,"opera"))
  18.  
    ||contains(ua,"ucweb7")||contains(ua,"iphone")){toMobileVertion();}
  19.  
    }
  20.  
    })();
  21.  
    function GetRequest() {
  22.  
    var url = location.search; //获取url中"?"符后的字串
  23.  
    var theRequest = new Object();
  24.  
    if (url.indexOf("?") != -1) {
  25.  
    var str = url.substr(1);
  26.  
    strs = str.split("&");
  27.  
    for(var i = 0; i < strs.length; i ++) {
  28.  
    theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
  29.  
    }
  30.  
    }
  31.  
    return theRequest;
  32.  
    }
  33.  
    </script>
  34.  
    <!---识别手机或电脑的js结束--->

 

 

最新资讯

技术文章

广告设计合作