通过获得到浏览器的 UserAgent 字符串可以得知 Internet Explorer 6-10 的 UserAgent 内容是这样的:
Internet Explorer 10 "5.0 (compatible; MSIE 10.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)" Internet Explorer 9 "5.0 (compatible; MSIE 9.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)" Internet Explorer 8 "4.0 (compatible; MSIE 8.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)" Internet Explorer 7 "4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)"
所以只需要用正则匹配响应的版本号即可判断当前的浏览器以及版本了。
var userAgent = window.navigator.userAgent.toLowerCase(); var ie10 = ($.browser.msie && /msie 10\.0/i.test(userAgent)); var ie9 = ($.browser.msie && /msie 9\.0/i.test(userAgent)); var ie8 = ($.browser.msie && /msie 8\.0/i.test(userAgent)); var ie7 = ($.browser.msie && /msie 7\.0/i.test(userAgent)); var ie6 = ($.browser.msie && /msie 6\.0/i.test(userAgent));