美文网首页
JS获取设备信息

JS获取设备信息

作者: 倚樓聽風雨 | 来源:发表于2021-03-10 14:45 被阅读0次

    获取设备类型名称:

    方法一:

    function browerType() {

        var sUserAgent = navigator.userAgent.toLowerCase();  //浏览器的用户代理设置为小写,再进行匹配

        var isIpad = sUserAgent.match(/ipad/i) == "ipad";  //或者利用indexOf方法来匹配

        var isIphoneOs = sUserAgent.match(/iphone os/i) == "iphone";

        var isMidp = sUserAgent.match(/midp/i) == "midp";  //移动信息设备描述MIDP是一套Java应用编程接口,多适用于塞班系统

        var isUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";  //CVS标签

        var isUc = sUserAgent.match(/ucweb/i) == "ucweb";

        var isAndroid = sUserAgent.match(/android/i) == "android";

        var isCe = sUserAgent.match(/windows ce/i) == "windows ce";

        var isWM = sUserAgent.match(/windows mobil/i) == "windows mobil";

        if (isIpad || isIphoneOs || isMidp || isUc7 || isUc || isAndroid || isCe || isWM) {

            alert('该设备为移动设备');

        } else {

            alert('该设备为PC设备');

        }

    }

    方法二:

    function detectOS() {

        var sUserAgent = navigator.userAgent;

        var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");

        var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");

        if (isMac) return "Mac";

         var isUnix = (navigator.platform == "X11") && !isWin && !isMac;

        if (isUnix) return "Unix";

        var isLinux = (String(navigator.platform).indexOf("Linux") > -1);

        var bIsAndroid = sUserAgent.toLowerCase().match(/android/i) == "android";

        if (isLinux) {

        if(bIsAndroid) return "Android";

        else return "Linux";

        }

        if (isWin) {

            var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1;

        if (isWin2K) return "Win2000";

        var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 ||

        sUserAgent.indexOf("Windows XP") > -1;

        if (isWinXP) return "WinXP";

        var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1;

        if (isWin2003) return "Win2003";

        var isWinVista= sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1;

        if (isWinVista) return "WinVista";

        var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1;

        if (isWin7) return "Win7";

        var isWin8 = sUserAgent.indexOf("Windows NT 6.2") > -1 || sUserAgent.indexOf("Windows 8") > -1;

        if (isWin8) return "Win8";

        var isWin9 = sUserAgent.indexOf("Windows NT 6.3") > -1 || sUserAgent.indexOf("Windows 9") > -1;

        if (isWin9) return "Win8.1";

        }

        return "other";

    }

    document.writeln("您的操作系统是:" + detectOS());

    alert(detectOS());

    相关文章

      网友评论

          本文标题:JS获取设备信息

          本文链接:https://www.haomeiwen.com/subject/fxvrqltx.html