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

前端 未结 14 1737
失恋的感觉
失恋的感觉 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:41

    Below is an improvement over James Padolsey's solution:

    1) It doesn't pollute memory (James' snippet creates 7 unremoved document fragments when detecting IE11, for example).
    2) It's faster since it checks for a documentMode value before generating markup.
    3) It's far more legible, especially to beginning JavaScript programmers.

    Gist link: https://gist.github.com/julianshapiro/9098609

    /*
     - Behavior: For IE8+, we detect the documentMode value provided by Microsoft.
     - Behavior: For  0; i--) {
                var div = document.createElement("div");
    
                div.innerHTML = "";
    
                if (div.getElementsByTagName("span").length) {
                    return i;
                }
            }
        }
    
        return undefined;
    })();
    

提交回复
热议问题