首页 > 代码库 > 利用$.support.leadingWhitespace检查浏览器是否为IE6-8

利用$.support.leadingWhitespace检查浏览器是否为IE6-8

$.support.leadingWhitespace为IE中特有的属性,因此可以利用$.support.leadingWhitespace来判断浏览器是否是IE6-8

$(function($){
            var ieFlag=  $.support.leadingWhitespace;//定义判断IE8的变量
              if(!ieFlag){//IE8以下
                 //IE代码
              }else{
                 //其他代码
              }
        });

/*与标题无关*/

jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support 。 在更新的 2.0 版本中,将不再支持 IE 6/7/8。 以后,如果用户需要支持 IE 6/7/8,只能使用 jQuery 1.9。 如果要全面支持 IE,并混合使用 jQuery 1.9 和 2.0,

官方的解决方案是:

1 <!--[if lt IE 9]>
2     <script src=http://www.mamicode.com/‘jquery-1.9.0.js‘></script>"color: #008080">3 <![endif]-->
4 <!--[if gte IE 9]>
5     <script src=http://www.mamicode.com/‘jquery-2.0.0.js‘></script>"color: #008080">6 <![endif]-->

 /*后发现的判断浏览器类型通用类型*/老外写的一篇文章,在IE、Firefox、Google下亲测可用

 1         // Firefox 1.0+
 2         var isFirefox = typeof InstallTrigger !== ‘undefined‘;
 3         alert("isFirefox:"+isFirefox);
 4         // Opera 8.0+
 5         var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(‘ OPR/‘) >= 0;
 6         alert("isOpera:"+isOpera);
 7         // Safari <= 9 "[object HTMLElementConstructor]"
 8         var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf(‘Constructor‘) > 0;
 9         alert("isSafari:"+isSafari);
10         // Internet Explorer 6-11
11         var isIE = /*@cc_on!@*/ false || !!document.documentMode;
12         alert("isIE:"+isIE);
13         // Edge 20+
14         var isEdge = !isIE && !!window.StyleMedia;
15         alert("isEdge:"+isEdge);
16         // Chrome 1+
17         var isChrome = !!window.chrome && !!window.chrome.webstore;
18         alert("isChrome:"+isChrome);
19         // Blink engine detection(7)
20         var isBlink = (isChrome || isOpera) && !!window.CSS;
21         alert("isBlink:"+isBlink);

英文分析,原文贴过来:Analysis of reliability

The previous method depended on properties of the rendering engine (-moz-box-sizing and -webkit-transform) to detect the browser. These prefixes will eventually be dropped, so to make detection even more robust, I switched to browser-specific characteristics:

  • Internet Explorer: JScript‘s Conditional compilation (up until IE9) and document.documentMode.
  • Edge: In Trident and Edge browsers, Microsoft‘s implementation exposes the StyleMediaconstructor. Excluding Trident leaves us with Edge.
  • Firefox: Firefox‘s API to install add-ons: InstallTrigger
  • Chrome: The global chrome object, containing several properties including a documented chrome.webstore object.
  • Safari: A unique naming pattern in its naming of constructors. This is the least durable method of all listed properties, because it‘s undocumented. On the other hand, there‘s no benefit in renaming the constructor, so it‘s likely to stay for a long while.
  • Opera: window.opera has existed for years, but will be dropped when Opera replaces its engine with Blink + V8 (used by Chromium).
    • Update 1: Opera 15 has been released, its UA string looks like Chrome, but with the addition of "OPR". In this version the chrome object is defined (but chrome.webstore isn‘t). Since Opera tries hard to clone Chrome, I use user agent sniffing for this purpose.
    • Update 2: !!window.opr && opr.addons can be used to detect Opera 20+ (evergreen).
  • Blink: CSS.supports() was introduced in Blink once Google switched on Chrome 28. It‘s of course the same Blink used in Opera.

利用$.support.leadingWhitespace检查浏览器是否为IE6-8