最近公司上线移动端,需要根据不同的系统跳转到不同的产品页面,百度后发现这一段代码很好用,不但可以判断当前是什么系统,还能知道当前浏览器是什么内核,移动端PC端都已测试无问题!

var browser = {
            versions: function() {
                var u = navigator.userAgent,app = navigator.appVersion;
                return { //移动终端浏览器版本信息
                    trident: u.indexOf('Trident') > -1, //IE内核
                    presto: u.indexOf('Presto') > -1, //opera内核
                    webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                    gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                    mobile: !! u.match(/AppleWebKit.*Mobile.*/) || !! u.match(/AppleWebKit/), //是否为移动终端
                    ios: !! u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                    android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
                    iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
                    iPad: u.indexOf('iPad') > -1, //是否iPad
                    webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部
                };
            }(),
            language: (navigator.browserLanguage || navigator.language).toLowerCase()
        }
        if (browser.versions.ios || browser.versions.iPhone || browser.versions.iPad) {
            window.location.href = "http://www.baidu.com";//想要跳转的地址
        } else if (browser.versions.android) {
            window.location.href = "http://www.hao123.com";//想要跳转的地址
        }

还有另一种简单写法,对于简单只需要判断是什么系统的可以试试下面的这种方法

if (/android/i.test(navigator.userAgent)) {
            window.location.href = "http://www.hao123.com";//想要跳转的地址
        }

if (/ipad|iphone|mac/i.test(navigator.userAgent)) {
            window.location.href = "http://www.baidu.com";//想要跳转的地址
        }

如果还工区分当前系统是移动端和PC端则需要用下面的方法,第一种方式有一个mobile 可以区分PC端跟移动端,但是在mac上面不起作用,会判断成移动端,所以得用下面的笨方法,判断目前主流的移动手持设备来区分移动跟PC端

<img src="jcwy.png" alt="" class="tips" id="tipsid"/>

var nowclass=browserRedirect();  if(nowclass.bIsIpad||nowclass.bIsIphoneOs||nowclass.bIsMidp||nowclass.bIsUc7||nowclass.bIsUc||nowclass.bIsAndroid||nowclass.bIsCE||nowclass.bIsWM){
            if(nowclass.bIsIphoneOs||nowclass.bIsIpad||nowclass.bIsMidp){//如果是ios移动设备
                window.location.href = "ios需要跳转的地方";
                if(nowclass.bIsWX){//解决以微信内核浏览器打开无法直接跳转给出提示点击跳转至safari打开
                    document.getElementById("tipsid").style.display="block";
                }
            }
            else if(nowclass.bIsAndroid){//如果是android移动设备
                window.location.href = "android需要跳转的地方";
            }
        }
        else{
            window.location.href = "PC端跳转去的地方";
        }
       function browserRedirect() {
            var sUserAgent = navigator.userAgent.toLowerCase();
            var systemclass={};
            systemclass.bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
            systemclass.bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
            systemclass.bIsMidp = sUserAgent.match(/midp/i) == "midp";
            systemclass.bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
            systemclass.bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
            systemclass.bIsAndroid = sUserAgent.match(/android/i) == "android";
            systemclass.bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
            systemclass.bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
            systemclass.bIsWX=sUserAgent.match(/MicroMessenger/i)=="micromessenger";
            return systemclass;
        }

最新文章

  1. Nodejs&#183;构建web应用
  2. 二、快速起步(Mysql镜像)
  3. EntityFramework执行SQL语句
  4. c# 基本知识 ref 和 out
  5. 28335 AD 转换
  6. dom4j处理java中xml还是很方便的
  7. H1B工作签证紧俏 “中签率”低对中国留学生影响大-中新网
  8. Facebook和Google如何激发工程师的创造力
  9. 『WPF』DataGrid的使用
  10. 从移动硬盘开机,引导VHD(Win10)
  11. 0323-DOM基础操作
  12. &#127827;vue &amp; react 一些重要但没必要死记硬背的东西
  13. MySQL 的安装
  14. 使用iframe框架后的页面跳转时目标页面变为iframe的子页面的问题
  15. 【Python】-NO.97.Note.2.Python -【Python 基本数据类型】
  16. [P2402] 奶牛隐藏
  17. 探索哪个进程使磁盘I/O升高
  18. SQL 中的LastIndexOf,截取最后一次出现字符后面的字符(转)
  19. .NET 开源GIS项目
  20. 在testbench从文件读入激励

热门文章

  1. bzoj 4753 [Jsoi2016]最佳团体——0/1分数规划
  2. Poj_1068 Parencodings
  3. 【246】◀▶IEW-Unit11
  4. Elasticsearch5.X Mapping详解
  5. DIV垂直水平居中
  6. Solr 6.7学习笔记(05)-- highlighter
  7. codevs1074 食物链
  8. 从图(Graph)到图卷积(Graph Convolution):漫谈图神经网络模型 (二)
  9. CODING 告诉你硅谷的研发项目管理之道(5)
  10. Kaggle 数据挖掘比赛经验分享