3

正确的利用javascript判断浏览器类型

 2 years ago
source link: https://www.geekjc.com/post/5e2d56f0ffcb864fb6c58260
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

正确的利用javascript判断浏览器类型

时间: 01/26/2020作者: ll浏览量: 391

直接看代码

// 获取当前浏览器类型
  getBrower = () => {
    const ua = window.navigator.userAgent.toLowerCase()

    // is IE
    if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
      return "IE";
    }
    // is Edge
    if (ua.match(/edge/) != null) {
      return "Edge";
    }
    // is UC
    if (ua.match(/ubrowser/) != null) {
      return "UC";
    }
    // is 搜狗
    if (ua.match(/metasr/) != null) {
      return "搜狗";
    }
    // is QQ
    if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
      return "QQ";
    }
    // is 360,360完全伪装,无法判断兼容模式
    if (ua.match(/chrome/) != null) {
      const is360 = this._mime(
        "type",
        "application/vnd.chromium.remoting-viewer"
      );
      if (is360) {
        return "360";
      }
      return "Chrome";
    }
  };

  _mime = (option, value) => {
    const mimeTypes = navigator.mimeTypes;
    for (const mt in mimeTypes) {
      if (mimeTypes[mt][option] === value) {
        return true;
      }
    }
    return false;
  };

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK