工具函数(判断浏览器版本)

;(function () {
  // 获取浏览器版本
function browserVersion() {
  // 取得浏览器的userAgent字符串
  var userAgent = navigator.userAgent;
  // 判断是否为小于IE11的浏览器
  var isLessIE11 = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1;
  // 判断是否为IE的Edge浏览器
  var isEdge = userAgent.indexOf('Edge') > -1 && !isLessIE11;
  // 判断是否为IE11浏览器
  var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1;
  // 判断是否为火狐浏览器
  var isFire = userAgent.indexOf('Mozilla') > -1 && userAgent.indexOf('Firefox') > -1;
  // 判断是否为谷歌浏览器
  var isChrome = userAgent.indexOf('Mozilla') > -1 && userAgent.indexOf('Chrome') > -1 && userAgent.indexOf('Edge') == -1;
  if (isLessIE11) {
      var IEReg = new RegExp('MSIE (\\d+\\.\\d+);');
      // 正则表达式匹配浏览器的userAgent字符串中MSIE后的数字部分,,这一步不可省略!!!
      IEReg.test(userAgent);
      // 取正则表达式中第一个小括号里匹配到的值
      var IEVersionNum = parseFloat(RegExp['$1']);
      if (IEVersionNum === 7) {
          // IE7
          return 'v-IE = 7'
      } else if (IEVersionNum === 8) {
          // IE8
          return 'v-IE = 8'
      } else if (IEVersionNum === 9) {
          // IE9
          return 'v-IE = 9'
      } else if (IEVersionNum === 10) {
          // IE10
          return 'v-IE = 10'
      } else {
          // IE版本<7
          return 'v-IE <= 6'
      }
  } else if (isEdge) {
      // edge
      var start = userAgent.indexOf('Edge')
      return "v-Edge = " + userAgent.substring(start + 5)
  } else if (isIE11) {
      // IE11
      return 'v-IE = 11'
  } else if (isFire) {
      // Firefox
      var start = userAgent.indexOf('Firefox')
      return "v-Firefox = " + userAgent.substring(start + 8)
  } else if (isChrome) {
      // Chrome
      var start = userAgent.indexOf('Chrome')
      return "v-Chrome = " + userAgent.substring(start + 7, start + 13)
  } else {
      // 不是主流浏览器
      return "browserInfo = " + userAgent
  }
}
console.log('browserVersion()', browserVersion())
})();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用JavaScript判断浏览器开发工具是否打开是一个常见的需求。虽然无法直接检测开发工具是否打开,但我们可以检测一些与开发工具相关的异常行为来间接判断其是否打开。 一种常用的方法是通过监听`window.console`对象是否被重写来判断。在浏览器开发工具中,通常会重写`console`对象,因此如果`console`对象发生了变化,就可以推断出开发工具可能被打开了。可以通过以下方式实现: ```javascript (function() { var devtoolsOpen = false; function checkDevTools() { if (window.console && window.console.firebug) { devtoolsOpen = true; } else if (window.console && window.console.profile) { devtoolsOpen = true; } else if (window.document.documentElement && window.document.documentElement.clientWidth == 0) { devtoolsOpen = true; } if (devtoolsOpen) { // 开发工具打开时的操作 console.log('开发工具已打开'); } } // 监听console对象是否被重写 setInterval(checkDevTools, 1000); })(); ``` 在以上代码中,我们通过定时调用`checkDevTools`函数来检测`console`对象是否被重写。如果`console`对象被重写,则推断开发工具可能被打开。 另外,还可以通过监听`keydown`事件来判断开发工具是否打开。在浏览器开发工具中,通常会禁用一些快捷键,如F12、Ctrl+Shift+I等。因此,我们可以在`keydown`事件中检测这些快捷键是否被触发,来间接判断开发工具是否打开。 ```javascript document.addEventListener('keydown', function(event) { if (event.keyCode === 123 || (event.ctrlKey && event.shiftKey && event.keyCode === 73)) { // 开发工具打开时的操作 console.log('开发工具已打开'); } }); ``` 通过以上两种方法,我们可以在JavaScript中较为准确地判断浏览器开发工具是否打开,从而进行一些相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值