web开发就不得不面对浏览器兼容性问题,特别是IE的兼容问题。在前端代码中经常要处理一些兼容格式,为了解决这个问题网上找了找识别浏览器版本的方法。
/**
* 获取浏览器版本
* @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;
}

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://www.cnblogs.com/5207/p/4846736.html