xwartz/xwartz.github.com

判断ie版本

xwartz opened this issue · 1 comments

最近在搞web app 优化,去掉些兼容性代码,单独拿出来给低版本ie用。

有个不错的方法判断ie版本

var isOldIE = function(){
       var b = document.createElement('b')
       b.innerHTML = '<!--[if lt IE 9 ]><i></i><![endif]-->'
       return b.getElementsByTagName('i').length === 1
}

通过ie特有的条件hack

更新:

var ie = (function(){
    // for-loop saves characters over while
    for( var v = 3,
             // b just as good as a div with 2 fewer characters
             el = document.createElement('b'),
             // el.all instead of el.getElementsByTagName('i')
             // empty array as loop breaker (and exception-avoider) for non-IE and IE10+
             all = el.all || [];
         // i tag not well-formed since we know that IE5-IE9 won't mind
         el.innerHTML = '<!--[if gt IE ' + (++v) + ']><i><![endif]-->',
         all[0];
       );
    // instead of undefined, returns the documentMode for IE10+ compatibility
    // non-IE will still get undefined as before
    return v > 4 ? v : document.documentMode;
}() );

https://gist.github.com/padolsey/527683#comment-786682