zoukankan      html  css  js  c++  java
  • JavaScript 浏览器类型及版本号

    项目中偶尔用到判断浏览器类型及相关版本问题,现记录相关代码:

            function getBrowserVertion(userAgent) {
    
                var browserName, browserVersion, types, Info;
                if (userAgent.indexOf("MSIE") > -1) {
                    types = userAgent.substring(userAgent.indexOf("MSIE"));
                    Info = (types.split(";")[0]).split(" ");
                    browserName = Info[0];
                    browserVersion = Info[1];
                } else if (userAgent.indexOf("Firefox") > -1) {
                    types = userAgent.substring(userAgent.indexOf("Firefox"));
                    Info = (types.split(" ")[0]).split("/");
                    browserName = Info[0];
                    browserVersion = Info[1];
                } else if (userAgent.indexOf("Chrome") > -1) {
                    types = userAgent.substring(userAgent.indexOf("Chrome"));
                    Info = (types.split(" ")[0]).split("/");
                    browserName = Info[0];
                    browserVersion = Info[1];
                } else if (userAgent.indexOf("Opera") > -1) {
                    types = userAgent.substring(userAgent.indexOf("Opera"));
                    Info = (types.split(" ")[0]).split("/");
                    browserName = Info[0];
                    browserVersion = Info[1];
                } else if (userAgent.indexOf("Safari") > -1) {
                    types = userAgent.substring(userAgent.indexOf("Safari"));
                    Info = (types.split(" ")[0]).split("/");
                    browserName = Info[0];
                    browserVersion = Info[1];
                } else if (userAgent.indexOf("rv") > -1) { // for IE11
                    var result = userAgent.substring(userAgent.indexOf("rv"), userAgent.indexOf(")"));
                    browserName = "MSIE";
                    browserVersion = result.split(":")[1];
                }
                if (browserVersion.indexOf('.') != -1) {
                    browserVersion = browserVersion.substring(0, browserVersion.indexOf('.'));
                }
                return browserName + "-" + browserVersion;
            }

      

    测试:

    getBrowserInfo(navigator.userAgent);
  • 相关阅读:
    FMDB增删查改
    https相关内容
    支付宝、微信支付参考博客
    下标脚本(Swift)
    函数(swift)
    控制流(swift)
    UIView Methods
    oc js 交互
    我和Lua并非一见钟情,我们期待着日久生情(相遇篇)
    与Python Falling In Love_Python跨台阶(面向对象)
  • 原文地址:https://www.cnblogs.com/itachy/p/7236219.html
Copyright © 2011-2022 走看看