Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to check if a person is using anything IE with version less than 8 or anything 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 in my js file the boolean like this:

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?

like image 1000
Badr Hari Avatar asked Apr 11 '11 22:04

Badr Hari


1 Answers

You need to declare the badIE variable as false first in order for it to work, or else the code outside of the conditional knows nothing about 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>
like image 200
Naftali Avatar answered Nov 15 '22 07:11

Naftali