Best way to check for IE less than 9 in JavaScript without library

前端 未结 14 1628
失恋的感觉
失恋的感觉 2020-11-28 21:10

What would be your fastest, shortest (best) way to detect browser which is IE and version less than 9 in JavaScript, without using jQuery or any add-on libraries?

相关标签:
14条回答
  • 2020-11-28 21:21

    You are all trying to overcomplicate such simple things. Just use a plain and simple JScript conditional comment. It is the fastest because it adds zero code to non-IE browsers for the detection, and it has compatibility dating back to versions of IE before HTML conditional comments were supported. In short,

    var IE_version=(-1/*@cc_on,@_jscript_version@*/);
    

    Beware of minifiers: most (if not all) will mistake the special conditional comment for a regular comment, and remove it

    Basically, then above code sets the value of IE_version to the version of IE you are using, or -1 f you are not using IE. A live demonstration:

    var IE_version=(-1/*@cc_on,@_jscript_version@*/);
    if (IE_version!==-1){
        document.write("<h1>You are using Internet Explorer " + IE_version + "</h1>");
    } else {
        document.write("<h1>You are not using a version of Internet Explorer less than 11</h1>");
    }

    0 讨论(0)
  • 2020-11-28 21:22

    I've decided to go with object detection instead.

    After reading this: http://www.quirksmode.org/js/support.html and this: http://diveintohtml5.ep.io/detect.html#canvas

    I'd use something like

    if(!!document.createElement('canvas').getContext) alert('what is needed, supported');
    
    0 讨论(0)
  • 2020-11-28 21:23

    This link contains relevant information on detecting versions of Internet Explorer:

    http://tanalin.com/en/articles/ie-version-js/

    Example:

    if (document.all && !document.addEventListener) {
        alert('IE8 or older.');
    }
    
    0 讨论(0)
  • 2020-11-28 21:24

    for what it's worth:

        if(  document.addEventListener  ){
            alert("you got IE9 or greater");
        }
    

    This successfully targets IE 9+ because the addEventListener method was supported very early on for every major browser but IE. (Chrome, Firefox, Opera, and Safari) MDN Reference. It is supported currently in IE9 and we can expect it to continue to be supported here on out.

    0 讨论(0)
  • 2020-11-28 21:24

    bah to conditional comments! Conditional code all the way!!! (silly IE)

    <script type="text/javascript">
    /*@cc_on
       var IE_LT_9 = (@_jscript_version < 9);
    @*/
    </script>
    

    Seriously though, just throwing this out there in case it suits you better... they're the same thing, this can just be in a .js file instead of inline HTML

    Note: it is entirely coincidental that the jscript_version check is "9" here. Setting it to 8, 7, etc will NOT check "is IE8", you'd need to lookup the jscript versions for those browsers.

    0 讨论(0)
  • 2020-11-28 21:28

    Javascript

    var ie = (function(){
    
        var undef,
            v = 3,
            div = document.createElement('div'),
            all = div.getElementsByTagName('i');
    
        while (
            div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
            all[0]
        );
    
        return v > 4 ? v : undef;
    
    }());
    

    You can then do:

    ie < 9
    

    By James Panolsey from here: http://james.padolsey.com/javascript/detect-ie-in-js-using-conditional-comments

    0 讨论(0)
提交回复
热议问题