zoukankan      html  css  js  c++  java
  • 封装判断浏览器是否是IE浏览器

    最近需求中,IE浏览器需要做一些特殊处理,所以封装了一个判断是否是IE浏览器的方法。

    // 获取浏览器内核,通过内核判断
    export const detectIE = () => {
      // 获取浏览器信息
      const ua = window.navigator.userAgent;
    
      // MSIE内核
      const msie = ua.indexOf('MSIE ');
      if (msie > 0) {
        return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
      }
    
      // Trident(又称为MSHTML) 代表:Internet Explorer
      const trident = ua.indexOf('Trident/');
      if (trident > 0) {
        const rv = ua.indexOf('rv:');
        return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
      }
    
      // Edge浏览器
      const edge = ua.indexOf('Edge/');
      if (edge > 0) {
        return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
      }
    
      return false;
    };
    // 判断浏览器是否存在
    export const isBrowser = () => typeof window !== 'undefined' && typeof window.document !== 'undefined';
    
    // 导出判断IE浏览器方法
    export const isIE = () => isBrowser && detectIE();
    

    调用:

    import { isIE } from '******';
    
    if(isIE()) {
        // to do somthing
    }
    
  • 相关阅读:
    一些博弈
    中国剩余定理分析及扩展
    2018年全国多校算法寒假训练营练习比赛(第三场)
    数论——逆元
    扩展欧几里得
    算法思维题
    匈牙利算法
    Codeforces #449 div2 C题
    16级C程序设计竞赛C题
    动态规划--模板--hdu 1059 Dividing
  • 原文地址:https://www.cnblogs.com/a-cat/p/12963737.html
Copyright © 2011-2022 走看看