javascript检测浏览器代码

学习来自:JavaScript高级程序设计(第4版) 

class BrowserDetector { 
    constructor() { 
    // 测试条件编译
    // IE6~10 支持
    this.isIE_Gte6Lte10 = /*@cc_on!@*/false; 
    // 测试 documentMode 
    // IE7~11 支持
    this.isIE_Gte7Lte11 = !!document.documentMode;
    // 测试 StyleMedia 构造函数
 // Edge 20 及以上版本支持
 this.isEdge_Gte20 = !!window.StyleMedia; 
 // 测试 Firefox 专有扩展安装 API 
 // 所有版本的 Firefox 都支持
 this.isFirefox_Gte1 = typeof InstallTrigger !== 'undefined'; 
 // 测试 chrome 对象及其 webstore 属性
 // Opera 的某些版本有 window.chrome,但没有 window.chrome.webstore 
 // 所有版本的 Chrome 都支持
 this.isChrome_Gte1 = !!window.chrome && !!window.chrome.webstore; 
 // Safari 早期版本会给构造函数的标签符追加"Constructor"字样,如:
 // window.Element.toString(); // [object ElementConstructor] 
 // Safari 3~9.1 支持
 this.isSafari_Gte3Lte9_1 = /constructor/i.test(window.Element); 
 // 推送通知 API 暴露在 window 对象上
 // 使用默认参数值以避免对 undefined 调用 toString() 
 // Safari 7.1 及以上版本支持
 this.isSafari_Gte7_1 = 
 (({pushNotification = {}} = {}) => 
 pushNotification.toString() == '[object SafariRemoteNotification]' 
 )(window.safari); 
 // 测试 addons 属性
 // Opera 20 及以上版本支持
 this.isOpera_Gte20 = !!window.opr && !!window.opr.addons; 
 } 
 isIE() { return this.isIE_Gte6Lte10 || this.isIE_Gte7Lte11; } 
 isEdge() { return this.isEdge_Gte20 && !this.isIE(); } 
 isFirefox() { return this.isFirefox_Gte1; } 
 isChrome() { return this.isChrome_Gte1; } 
 isSafari() { return this.isSafari_Gte3Lte9_1 || this.isSafari_Gte7_1; } 
 isOpera() { return this.isOpera_Gte20; } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值