小型JavaScript武器库

  • 2017-11-12
  • 123
  • 0

测试网页:http://www.waylee.net/p.html

代码出处:https://my.oschina.net/u/2362038/blog/652613

JS判断案例:

——————————–判断操作系统———————————

 function detectOS() {
        var sUserAgent = navigator.userAgent;
        //return navigator.userAgent;
        var isWin = (navigator.platform == "Win32"||navigator.platform == "Win64") || (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 isAndroid = (sUserAgent.indexOf('Android') > -1);
        if (isAndroid) return "Android";
        var isLinux = (String(navigator.platform).indexOf("Linux") > -1);
        if (isLinux) return "Linux";
        var isIos = (sUserAgent.indexOf('iPhone') > -1 || sUserAgent.indexOf('iPad') > -1 || sUserAgent.indexOf('iPod') > -1);
        if (isIos) return "iOS";
        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 isWin8_1 = sUserAgent.indexOf("Windows NT 6.3") > -1 || sUserAgent.indexOf("Windows 8.1") > -1;
            if (isWin8_1) return "Win8.1";
            var isWin10 = sUserAgent.indexOf("Windows NT 10.0") > -1 || sUserAgent.indexOf("Windows 10") > -1;
            if (isWin10) return "Win10";
            var isWin10_1 = sUserAgent.indexOf("Windows NT 10.1") > -1 || sUserAgent.indexOf("Windows10 Update 1") > -1;
            if (isWin10_1) return "Windows10 Update 1";
            var isWin10_2 = sUserAgent.indexOf("Windows NT 10.2") > -1 || sUserAgent.indexOf("Windows10 redstone") > -1;
            if (isWin10_2) return "Windows10 redstone";
        }
        return "other";
    }
    document.writeln("您的操作系统是:" + detectOS());

 

——————————-判断浏览器类型——————————–

function getExplorer() {
 var explorer = window.navigator.userAgent;
 //ie 
 if (explorer.indexOf("MSIE") >= 0) {
 return "IE";
 }
 //firefox 
 else if (explorer.indexOf("Firefox") >= 0) {
 return "Firefox";
 }
 //Chrome
 else if (explorer.indexOf("Chrome") >= 0) {
 return "Chrome";
 // console.log("Chrome")
 }
 //Opera
 else if (explorer.indexOf("Opera") >= 0) {
 return "Opera";
 }
 //Safari
 else if (explorer.indexOf("Safari") >= 0) {
 return "Safari";
 }
}
getExplorer();
document.writeln("您的浏览器类型是:" + getExplorer());

——————————–判断设备——————————–

function browserRedirect() {
      var sUserAgent = navigator.userAgent.toLowerCase();
      var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
      var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
      var bIsMidp = sUserAgent.match(/midp/i) == "midp";
      var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
      var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
      var bIsAndroid = sUserAgent.match(/android/i) == "android";
      var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
      var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
      document.writeln("您的浏览设备为:");
      if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
        document.writeln("Phone");
      } else {
        document.writeln("PC");
      }
    }

    browserRedirect();

 

 

 

  1. 2016:  [經驗] 凶殺案託夢 - 第二個夢境(0)
  2. 2016:  【討論】SCP-CN-486 求救信(0)

评论

偷偷告诉你,这还毛都没有 T T

发表评论