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

I am trying to check if a person is using something IE with version less than 8 or something else.

I use conditional comments to declare boolean ..

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

And now I check my js file as a boolean:

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

If I use IE7 or IE6, it warns that "you have bad IE!". If I use anything else, he should warn “Bueno!”, But it’s not. What is the problem?

+6
source share
3 answers

You need to declare the badIE variable as false first so that it works, otherwise the code outside the conditional character knows nothing about badIE

Try the following:

 <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> 
+20
source

Not a direct answer (Neil covers you), but you might also be interested in: http://code.google.com/p/ie7-js/

On 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 correct in IE5 and IE6.

Although if you use jQuery, keep in mind that it does many of the same things as this library.

+5
source

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

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

+2
source

Source: https://habr.com/ru/post/885683/


All Articles