欢迎登陆真网站,您的到来是我们的荣幸。 登陆 注册 忘记密码? ☆设为首页 △加入收藏
欢迎加入真幸福QQ群
电脑知识: 基础知识 网络技术 操作系统 办公软件 电脑维修 电脑安全 windows7 windows8 windows10 服务器教程 平板电脑 视频播放教程 网络应用 互联网 工具软件 浏览器教程 QQ技巧 输入法教程 影视制作 YY教程 wps教程 word教程 Excel教程 PowerPoint
云南西双版纳特产小花糯玉米真空包装


爱奇艺白银会员和黄金会员有什么区别?
怎么设置Word2013启动时不显示开始屏幕
怎么在Word2013中制作语文作文稿纸
IE浏览器流氓插件删除工具
斗鱼TV一个点赞的价格是多少
会声会影x8在图片上播视频的三种方法
Word2013如何添加图片水印以和文字水印
在IE浏览器中正常显示的字体效果设置
斗鱼tv666多少钱?
会声会影转场特效的运用技巧
IE浏览器检测的方法
【 来源:网络 】【 点击:1 】 【 发布时间:2017_03_03 08:59:59 】

   传统一般都是采用user-agent的检测方式,它检测目标浏览器的确切型号,包括浏览器的名称和版本。其实就是一个字符串,用navigator.userAgen或navigator.appName获取。如下:

  function isIE(){

  return navigator.appName.indexOf("Microsoft Internet Explorer")!=-1 && document.all;

  }

  function isIE6() {

  return navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie 6.0")=="-1"?false:true;

  }

  function isIE7(){

  return navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie 7.0")=="-1"?false:true;

  }

  function isIE8(){

  return navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie 8.0")=="-1"?false:true;

  }

  function isNN(){

  return navigator.userAgent.indexOf("Netscape")!=-1;

  }

  function isOpera(){

  return navigator.appName.indexOf("Opera")!=-1;

  }

  function isFF(){

  return navigator.userAgent.indexOf("Firefox")!=-1;

  }

  function isChrome(){

  return navigator.userAgent.indexOf("Chrome") > -1;

  }

  目前比较流行的检测方式则是通过“对象/特征”来判断浏览器能力的方式。下面的addEvent代码就是使用了该方式。.addEventListener是w3c dom标准方式,而IE使用自己特有attachEvent。

  //添加事件工具函数

  function addEvent(el,type,handle){

  if(el.addEventListener){//for standard browses

  el.addEventListener(type,handle,false);

  }else if(el.attachEvent){//for IE

  el.attachEvent("on"+event,handle);

  }else{//other

  el["on"+type]=handle;

  }

  }

  在举几个列子:

  1、talbe.cells只有IE/Opera支持。

  2、innerText/insertAdjacentHTML除Firefox外,IE6/7/8/Safari/Chrome/Opera都支持。

  3、window.external.AddFavorite用来在IE下添加到收藏夹。

  4、window.sidebar.addPanel用来在Firefox下添加到收藏夹。

  还有一种有趣的检测方式,暂且称为“浏览器缺陷或bug”方式,即某些表现不是浏览器厂商刻意实现的。如下:

  var isIE = !+"v1";

  var isIE = !-[1,];

  var isIE = "v"=="v";

  isSafari=/a/.__proto__=='//';

  isOpera=!!window.opera;

  isIE = "v" == "v" 方式IE9已经修复该bug,不能用此方式判断IE浏览器了(2010-6-29用IE9平台预览第三版测试的)

  最经典的莫过于 !-[1,] 的判断方式,仅仅用了6 bytes判定IE浏览器,创世界最短纪录,这组代码利用了数组[1,]的length。

  IE浏览器检测代码:

  // ----------------------------------------------------------

  // A short snippet for detecting versions of IE in JavaScript

  // without resorting to user-agent sniffing

  // ----------------------------------------------------------

  // If you're not in IE (or IE version is less than 5) then:

  // ie === undefined

  // If you're in IE (>=5) then you can determine which version:

  // ie === 7; // IE7

  // Thus, to detect IE:

  // if (ie) {}

  // And to detect the version:

  // ie === 6 // IE6

  // ie > 7 // IE8, IE9 ...

  // ie < 9 // Anything less than IE9

  // ----------------------------------------------------------

  //UPDATE: Now using Live NodeList idea from @jdalton

  var ie = (function() {

  var undef,

  v = 3,

  div = document.createElement('div'),

  all = div.getElementsByTagName('i');

  while (

  div.innerHTML = '<!--[if gt IE ' + (++v) + ']><![endif]-->',

  all[0]

  );

  return v > 4 ? v : undef;

  })();

  这个代码的意图就是.通过while循环.将条件编译写入到div即页面中,然后直接取注释下的这个元素,直到取不到就退出循环。这样就获得了IE的最终版本,如果不是IE那么就是undefined。

本网站由川南居提供技术支持,fkzxf版权所有 浙ICP备12031891号
淳安分站 淳安分站