星空网 > 软件开发 > 网页设计

学习笔记:IE版本判断新方法

web开发就不得不面对浏览器兼容性问题,特别是IE的兼容问题。在前端代码中经常要处理一些兼容格式,为了解决这个问题网上找了找识别浏览器版本的方法。

 

常规js方法

找到一个方法,还不错,可以识别出各种浏览器的版本:
 
/** * 获取浏览器版本 * @returns {Array} */function browserVersion() {  ua = navigator.userAgent;  ua = ua.toLocaleLowerCase();   if (ua.match(/msie/) != null || ua.match(/trident/) != null) {    browserType = "IE";    //哈哈,现在可以检测ie11.0了!    browserVersion = ua.match(/msie ([\d.]+)/) != null ? ua.match(/msie ([\d.]+)/)[1] : ua.match(/rv:([\d.]+)/)[1];  } else if (ua.match(/firefox/) != null) {    browserType = "火狐";  } else if (ua.match(/opera/) != null) {    browserType = "欧朋";  } else if (ua.match(/chrome/) != null) {    browserType = "谷歌";  } else if (ua.match(/safari/) != null) {    browserType = "Safari";  }  var arr = new Array(browserType, browserVersion);  return arr;}

   


在代码中直接使用也比较简单了。
 

IE的条件注释

在一次偶然的机会看网金社的前端代码,发现一段很特别的注释:
学习笔记:IE版本判断新方法
 
这个貌似有点神奇,看代码的意思是一个跳转,但显示是注释的语句,于是抱着试一试的态度将浏览器切换到IE低版本,果然是有效果的。赶紧网上开始找这方面的资料,有一些用法说明,看来确实是可行的方案。
 
条件注释属性:

gt : greater than,选择条件版本以上版本,不包含条件版本

lt : less than,选择条件版本以下版本,不包含条件版本

gte : greater than or equal,选择条件版本以上版本,包含条件版本

lte : less than or equal,选择条件版本以下版本,包含条件版本

! : 选择条件版本以外所有版本,无论高低

用法:

<!--[if IE]>用于 IE <![endif]-->

<!--[if IE 6]>用于 IE6 <![endif]-->

<!--[if IE 7]>用于 IE7 <![endif]-->

<!--[if IE 8]>用于 IE8 <![endif]-->

<!--[if IE 9]>用于 IE9 <![endif]-->

<!--[if gt IE 6]> 用于 IE6 以上版本<![endif]-->

<!--[if lte IE 7]> 用于 IE7或更低版本 <![endif]-->

<!--[if gte IE 8]>用于 IE8 或更高版本 <![endif]-->

<!--[if lt IE 9]>用于 IE9 以下版本<![endif]-->

<!--[if !IE]> -->用于非 IE <!-- <![endif]-->


参考:http://baike.sogou.com/v66165627.htm



原标题:学习笔记:IE版本判断新方法

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流