Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用document.addEventListener使用JavaScript检测IE8_Javascript_Internet Explorer 8_Conditional Operator_Browser Detection - Fatal编程技术网

使用document.addEventListener使用JavaScript检测IE8

使用document.addEventListener使用JavaScript检测IE8,javascript,internet-explorer-8,conditional-operator,browser-detection,Javascript,Internet Explorer 8,Conditional Operator,Browser Detection,我在一个项目中遇到了这段代码,该项目用于检测浏览器是否为IE8: var isIE8 = function(){ return (!document.addEventListener) ? true : false; }; 我不明白为什么条件运算符?true:false用于在true时返回true,在false时返回false。逻辑代码不是应用于文档的值。addEventListener将始终返回一个布尔值,这不是做完全相同的事情吗: var isIE8 = function(){ retur

我在一个项目中遇到了这段代码,该项目用于检测浏览器是否为IE8:

var isIE8 = function(){ return (!document.addEventListener) ? true : false; };
我不明白为什么条件运算符
?true:false
用于在true时返回true,在false时返回false。逻辑代码不是
应用于
文档的值。addEventListener
将始终返回一个布尔值,这不是做完全相同的事情吗:

var isIE8 = function(){ return (!document.addEventListener); };

这是我能找到的最接近的信息


感谢您查看:)

如果您只想检测IE8,请检查
addEventListener
即可。但是,如果您想查看IE及其一般版本,我认为这种方法最直观、最有用:

var uA = navigator.userAgent;
var browser = null;
var ieVersion = null; 

if (uA.indexOf('MSIE 6') >= 0) {
    browser = 'IE';
    ieVersion = 6;
}
if (uA.indexOf('MSIE 7') >= 0) {
    browser = 'IE';
    ieVersion = 7;
}
if (document.documentMode) { // as of IE8
    browser = 'IE';
    ieVersion = document.documentMode;
}
您还可以通过这种方式在兼容模式/视图中捕获更高的IE版本。使用脚本非常简单:

if ((browser == 'IE') && (ieVersion <= 10)) 
    // do something, for example:
    document.documentElement.className += ' ie10-'; 

if((浏览器='IE')&&(ieVersion我看不出三元运算符的用途。它可能只是!!相反。可能原始编码人员认为它更容易阅读。我建议完全删除此标志并为功能而不是浏览器编码。作为旁注,检查是否存在
addEventListener
是检测IE8的一种不好的方法,inserting条件语句在我看来会更好。但是,这是确定是否存在
addEventListener
的一个好方法,如果不尝试
attachEvent
,那么名称isIE8将有点误导。换句话说,在这种情况下,只有编写代码的人才能回答。我们所能做的就是猜测。@KevinB
!!
会在IE8中将变量isIE8设置为false
会更有意义。正如我所怀疑的那样,似乎没有理由这样做。感谢@adeneo的输入,我同意变量名称是误导性的。这是检测IE版本的一个很好的实现,谢谢。在重新阅读我的问题后,我意识到它更容易出错我们不知道使用条件运算符从已经是布尔值的值返回布尔值背后的想法,也就是说,很抱歉。