使用条件注释和 javascript 检测 IE7 或更低版本是否不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5628146/
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-25 17:49:40  来源:igfitidea点击:

Detecting if IE7 or lower with conditional comments and javascript not working

javascriptinternet-explorer

提问by Badr Hari

I'm trying to check if a person is using anything IE with version less than 8 or anything else.

我正在尝试检查一个人是否正在使用版本低于 8 的任何 IE 或其他任何东西。

I use conditional comments to declare boolean..

我使用条件注释来声明布尔值..

<!--[if lt IE 8]>
<script type="text/javascript">var badIE = true;</script>
<![endif]-->

And now I check in my js file the boolean like this:

现在我在我的 js 文件中检查布尔值,如下所示:

if (badIE == true){
    alert('You have bad IE!');
} else {
    alert('Bueno!');
}

If I use IE7 or IE6, it alerts that "You have bad IE!". If I use anything else, it should alert "Bueno!", but it does not. What is the problem?

如果我使用 IE7 或 IE6,它会警告“你的 IE 很糟糕!”。如果我使用其他任何东西,它应该提醒“Bueno!”,但它没有。问题是什么?

回答by Naftali aka Neal

You need to declare the badIEvariable as false first in order for it to work, or else the code outside of the conditional knows nothingabout badIE

需要声明的badIE变量为假第一顺序为它工作,或者有条件的代码之外知道什么有关badIE

Try this:

试试这个:

<script>
    var badIE = false;
</script>

<!--[if lt IE 8]>
<script type="text/javascript">badIE = true;</script>
<![endif]-->

<script>
if (badIE == true){
    alert('You have bad IE!');
} else {
    alert('Bueno!');
}
</script>

回答by entropo

Not a direct answer (Neal has you covered), but you may also be interested in: http://code.google.com/p/ie7-js/

不是一个直接的答案(Neal 有你介绍过),但你可能也有兴趣:http: //code.google.com/p/ie7-js/

From the page:

从页面:

IE7.js is a JavaScript library to make Microsoft Internet Explorer behave like a standards-compliant browser. It fixes many HTML and CSS issues and makes transparent PNG work correctly under IE5 and IE6.
IE7.js 是一个 JavaScript 库,用于使 Microsoft Internet Explorer 表现得像符合标准的浏览器。它修复了许多 HTML 和 CSS 问题,并使透明 PNG 在 IE5 和 IE6 下正常工作。

Though if you're using jQuery, keep in mind that it does many of the same thingsas this library.

但是,如果您使用的是 jQuery,请记住它与该库执行许多相同的操作

回答by Devin Burke

Your conditional statement <!--[if lt IE 8]> ... <![endif]-->means that the code between those will not be executed if the web browser is not an IE version less than 8, which means var badIEwill never be declared for all other browsers (i.e., FireFox, IE8, IE9, Safari, etc.)

您的条件语句<!--[if lt IE 8]> ... <![endif]-->意味着如果 Web 浏览器不是低于 8 的 IE 版本,则不会执行它们之间的代码,这意味着var badIE永远不会为所有其他浏览器(即 FireFox、IE8、IE9、Safari 等)声明

Because it is never declared, you are getting a silent scripting error in the background when the browser tries to execute if (badIE == true){, which means the browser immediately stops reading the script and does not try to execute the alert('Bueno!');statement.

因为它从未被声明,所以当浏览器尝试执行时,您会在后台收到一个静默的脚本错误if (badIE == true){,这意味着浏览器会立即停止读取脚本并且不会尝试执行该alert('Bueno!');语句。