简体   繁体   English

如何用javascript检测IE6?

[英]How detect IE6 with javascript?

How I do to detect the internet explorer and lower versions using javascript? 我如何使用javascript检测Internet Explorer和更低版本? I know about the navigator object, but what is the information that it send to identify him? 我知道导航器对象,但它发送给他的信息是什么?

I recommend using conditional comments to the load the CSS or JS targeted for IE xx as @Jason McCreary suggests. 我建议使用条件注释来加载IE xx的IE或JS,正如@Jason McCreary建议的那样。 However, if you're deep in the code of a JS plug-in or something and need it for JS, this is a pretty sure fire way of getting it done: 但是,如果你深入研究JS插件的代码或需要它来为JS,那么这是一个非常肯定的完成它的方法:

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

And if you're a minimalist, a bit of a rebel; 如果你是一个极简主义者,有点反叛者; this method I believe actually works: 这个方法我认为实际上有效:

if (typeof document.body.style.maxHeight != "undefined") {
    // IE 7, moz, saf, opera 9
    } else{
    // IE6, older browsers
}

If you have access to the code, I suggest conditional comments . 如果您有权访问该代码,我建议您使用条件注释 It's 100% and less JavaScript. 这是100%而不是JavaScript。

<!--[if IE 6]>
<script type="text/javascript">
var IE6 = true;
</script>
<![endif]-->

If not, traditional JavaScript methods include analyzing navigator or other object detection to sniff out the browser. 如果没有, 传统的 JavaScript方法包括分析navigator或其他对象检测以嗅出浏览器。

Conditional comments is easily the best approach, as suggested by Jason McCreary. 正如Jason McCreary所建议的那样,条件评论很容易成为最好的方法。 It is possible to use conditional comments with just JavaScript as follows: 可以使用JavaScript的条件注释,如下所示:

var div = document.createElement("div");
div.innerHTML = "<!--[if lte IE 6]><i></i><![endif]-->";
var isIe6orLower = !!div.getElementsByTagName("i").length;

alert("Is IE 6 or lower: " + isIe6orLower);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM