JavaScript 使用原始Javascript检测浏览器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 使用原始Javascript检测浏览器相关的知识,希望对你有一定的参考价值。

/*
 * Andrew Wayne 2010
 * Browser detection with javascript
 * this has been passed through JSLint
*/

var Browser = {
  init: function () {
    // Get the browser info
    this.browser = this.searchString(this.dataBrowser) || "unknownBrowser";
    this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "unknownVersion";
    this.OS = this.searchString(this.dataOS) || "unknownOS";
  },
  
  // search through the dataBrowser and dataOS object to find a match
  // for the browser name and OS respectively
  searchString: function (data) {
    var dataString, dataProp, i=0, len = data.length;
    for (i; i<len; i+=1) {
      dataString = data[i].string;
      dataProp = data[i].prop;
      this.versionSearchString = data[i].versionSearch || data[i].agent;
      if (dataString) {
        if (dataString.indexOf(data[i].subString) !== -1) {
            return data[i].identity;
          }
      }
      else if (dataProp) {
        return data[i].identity;
      }
    }
  },
  
  // search through the userAgent to find a match
  // for the browser version
  searchVersion: function (dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index === -1) { return; }
    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
  },

  dataBrowser: [
    {
      string: navigator.userAgent,
      subString: "Chrome",
      agent: "Chrome",
      identity: "chrome"
    },
    {
      string: navigator.userAgent,
      subString: "OmniWeb",
      versionSearch: "OmniWeb/",
      agent: "OmniWeb",
      identity: "omniweb"
    },
    {
      string: navigator.vendor,
      subString: "Apple",
      agent: "Safari",
      identity: "safari",
      versionSearch: "Version"
    },
    {
      prop: window.opera,
      agent: "Opera",
      identity: "opera"
    },
    {
      string: navigator.vendor,
      subString: "iCab",
      agent: "iCab",
      identity: "icab"
    },
    {
      string: navigator.vendor,
      subString: "KDE",
      agent: "Konqueror",
      identity: "konqueror"
    },
    {
      string: navigator.userAgent,
      subString: "Firefox",
      agent: "Firefox",
      identity: "ff"
    },
    {
      string: navigator.vendor,
      subString: "Camino",
      agent: "Camino",
      identity: "camino"
    },
    {
      // for newer Netscapes (6+)
      string: navigator.userAgent,
      subString: "Netscape",
      agent: "Netscape",
      identity: "netscape"
    },
    {
      string: navigator.userAgent,
      subString: "MSIE",
      agent: "Explorer",
      identity: "ie",
      versionSearch: "MSIE"
    },
    {
      string: navigator.userAgent,
      subString: "Gecko",
      agent: "Mozilla",
      identity: "mozilla",
      versionSearch: "rv"
    },
    {
      // for older Netscapes (4-)
      string: navigator.userAgent,
      subString: "Mozilla",
      agent: "Netscape",
      identity: "netscape",
      versionSearch: "Mozilla"
    }
  ],
  dataOS : [
    {
      string: navigator.platform,
      subString: "Win",
      agent: "Windows",
      identity: "win"
    },
    {
      string: navigator.platform,
      subString: "Mac",
      agent: "Mac",
      identity: "mac"
    },
    {
       string: navigator.userAgent,
       subString: "iPhone",
       agent: "iPhone/iPod",
       identity: "iphone-ipod"
    },
    {
      string: navigator.platform,
      subString: "Linux",
      agent: "Linux",
      identity: "linux"
    }
  ]
};

// Example usage
Browser.init();
document.write('<h1>You\'re using ' + Browser.browser + ' ' + Browser.version + ' on ' + Browser.OS + '!</h1>');

以上是关于JavaScript 使用原始Javascript检测浏览器的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript:onkeyup() 的原始目标

使用原始Javascript加载XML

如何在 JavaScript 中反转数组,同时保留原始数组的原始值? [复制]

使用原始 UInt16Array 值 javascript 渲染到画布

使用 WebSocket 的原始 javascript GraphQL 订阅不起作用

使用JavaScript从JPEG访问原始YUV值