在没有库的情况下在JavaScript中检查小于9的最佳方法

什么是最快,最短(最好)的方式来检测浏览器是IE和JavaScript版本低于9,而不使用jQuery或任何附加库?

使用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; }()); 

你可以这样做:

 ie < 9 

James Panolsey来自这里: http ://james.padolsey.com/javascript/detect-ie-in-js-using-conditional-comments

物有所值:

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

这成功地针对IE 9 +,因为除了IE之外,每个主要的浏览器都很早就支持addEventListener方法。 (Chrome,Firefox,Opera和Safari) MDN参考 。 它目前在IE9中得到了支持,我们可以期待它在这里继续得到支持。

使用条件注释,您可以创build一个脚本块,只能在IE中执行less于9。

 <!--[if lt IE 9 ]> <script> var is_ie_lt9 = true; </script> <![endif]--> 

当然,你可以在这个块之前添加一个声明var is_ie_lt9=false的通用块,这会覆盖IE小于9的地方。(在这种情况下,你想删除var声明,因为它是重复的) 。

编辑 :这是一个不依赖于内嵌脚本块(可以从外部文件运行),但不使用用户代理嗅探的版本:

通过@cowboy :

 with(document.createElement("b")){id=4;while(innerHTML="<!--[if gt IE "+ ++id+"]>1<![endif]-->",innerHTML>0);var ie=id>5?+id:0} 

b有条件的评论! 有条件的代码一路!!! (愚蠢的IE)

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

严重的是,只是扔在那里,以防万一它适合你更好…他们是同样的事情,这可以只是在一个.js文件,而不是内联的HTML

注意:在这里jscript_version检查是“9”完全是巧合。 设置为8,7等不会检查“是IE8”,你需要查找这些浏览器的jscript版本。

以下是对James Padolsey解决scheme的改进:

1)不污染记忆(例如,当检测IE11时,James的片段创build了7个未删除的文档片段)。
2)在生成标记之前检查documentMode的值会更快。
3)更清晰,特别是对于开始JavaScript程序员。

要点链接: https : //gist.github.com/julianshapiro/9098609

 /* - Behavior: For IE8+, we detect the documentMode value provided by Microsoft. - Behavior: For <IE8, we inject conditional comments until we detect a match. - Results: In IE, the version is returned. In other browsers, false is returned. - Tip: To check for a range of IE versions, use if (!IE || IE < MAX_VERSION)... */ var IE = (function() { if (document.documentMode) { return document.documentMode; } else { for (var i = 7; i > 0; i--) { var div = document.createElement("div"); div.innerHTML = "<!--[if IE " + i + "]><span></span><![endif]-->"; if (div.getElementsByTagName("span").length) { return i; } } } return undefined; })(); 
 var ie = !-[1,]; // true if IE less than 9 

这个黑客支持ie5,6,7,8。 它固定在ie9 +(所以它适合这个问题的要求)。 这个黑客工作在所有IE兼容模式。

它是如何工作的 :即引擎将空元素数组(如[,1])作为具有两个元素的数组来处理,而其他浏览器则认为只有一个元素。 所以当我们用+运算符把这个数组转换成数字时,我们做了这样的事情:(',1'在其他人的ie /'1'中)* 1,我们在ie中得到NaN,在其他人中得到1。 比我们将它转​​换为布尔值并且使用! 简单。 顺便我们可以使用更短的版本没有! 标志,但价值将被扭转。

这是迄今为止最短的黑客攻击。 我是作者;)

我决定改用对象检测。

阅读后: http : //www.quirksmode.org/js/support.html和这个: http : //diveintohtml5.ep.io/detect.html#canvas

我会使用类似的东西

 if(!!document.createElement('canvas').getContext) alert('what is needed, supported'); 

此链接包含有关检测Internet Explorer版本的相关信息:

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

例:

 if (document.all && !document.addEventListener) { alert('IE8 or older.'); } 

你可以用一个正则expression式和.match()快速和肮脏的方式:

 if (navigator.userAgent.match(/MSIE\s(?!9.0)/)) { // ie less than version 9 } 

如果我是你,我会使用条件编译或function检测。
这是另一种select:

 <!--[if lt IE 9]><!--> <script> var LTEIE8 = true; </script> <!--<![endif]--> 

是否需要在JavaScript中完成?

如果没有,那么你可以使用特定于IE的条件注释语法:

 <!--[if lt IE 9]><h1>Using IE 8 or lower</h1><![endif]--> 

我喜欢迈克·刘易斯的回答,但代码没有通过jslint,我无法理解时髦的循环。 我的使用案例是,如果IE8小于或等于IE8,则不支持浏览器消息。

这是一个基于Mike Lewis的jslint免费版本:

 /*jslint browser: true */ /*global jQuery */ (function () { "use strict"; var browserNotSupported = (function () { var div = document.createElement('DIV'); // http://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx div.innerHTML = '<!--[if lte IE 8]><I></I><![endif]-->'; return div.getElementsByTagName('I').length > 0; }()); if (browserNotSupported) { jQuery("html").addClass("browserNotSupported").data("browserNotSupported", browserNotSupported); } }()); 
 if (+(/MSIE\s(\d+)/.exec(navigator.userAgent)||0)[1] < 9) { // IE8 or less } 
  • 提取IE版本:/ /MSIE\s(\d+)/.exec(navigator.userAgent) /. /MSIE\s(\d+)/.exec(navigator.userAgent)
  • 如果它是非IE浏览器,这将返回null所以在这种情况下, ||0将把null切换到0
  • [1]会得到主要版本的IE或undefined如果它不是一个IE浏览器
  • 前面的+会把它转换成一个数字, undefined会被转换成NaN
  • 比较NaN和一个数字将总是返回false

你们都试图把这些简单的东西过分复杂化。 只需使用简单而简单的JScript条件注释。 这是最快的,因为它将零代码添加到非IE浏览器中进行检测,并且在支持HTML条件注释之前,它具有可追溯到IE版本的兼容性。 简而言之,

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

小心minifiers:大多数(如果不是全部)将错误的特殊条件注释的正则评论,并删除它

基本上,然后上面的代码将IE_version的值设置为您正在使用的IE版本,或者-1f您不使用IE。 现场演示:

 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>"); }