检查 IE8 是否使用纯 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21855378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:51:19  来源:igfitidea点击:

Check if IE8 with pure Javascript

javascriptjqueryinternet-explorer-8

提问by Toni Michel Caubet

I used to check it this way:

我以前是这样检查的:

$.browser.msie && $.browser.version == 8

But it seems that $.browserhas been removed from the later versinos of jQuery,

但似乎$.browser已从 jQuery 的后期版本中删除,

So,

所以,

How can I check that with just pure javascript?

我怎样才能只用纯 javascript 来检查?

I tried:

我试过:

isIE8: navigator.appVersion.indexOf("MSIE 8") > 0

isIE8: navigator.appVersion.indexOf("MSIE 8") > 0

Wich seems to Do it, but it doesn't look that good and actually looks like it have many flaws...

Wich 似乎做到了,但它看起来并不那么好,实际上看起来它有很多缺陷......

Any better aproach?

有什么更好的方法吗?

采纳答案by Ronny

This is longer than the one-liner you probably want, but safer than parsing the UA string, because it's based on actual behaviour (IE conditional comments): https://gist.github.com/paulirish/357741

这比您可能想要的单行更长,但比解析 UA 字符串更安全,因为它基于实际行为(IE 条件注释):https: //gist.github.com/paulirish/357741

回答by davidkonrad

You could check if the SVGtag is supported. SVGwas not supported in IE8 -> http://caniuse.com/#search=svgTo the opposit, the DATAtag was introduced in IE8 -> http://caniuse.com/#search=data

您可以检查是否SVG支持该标签。SVGIE8 不支持 -> http://caniuse.com/#search=svg反之,DATA标签是在 IE8 中引入的 -> http://caniuse.com/#search=data

So :

所以 :

if (navigator.userAgent.toLowerCase().indexOf('msie') != -1) {
   if (!document.createElement('SVG').getAttributeNS) {
       if (document.createElement('DATA').getAttributeNS) {
           //the browser is defently IE8
        }
    }
}