Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: change based on whether IE7 or not

I would like to change a line of my javascript code based on whether the browser is IE7 or not. Here is the code for any other browser:

function showHint(myId) 
{
    document.getElementById(myId).style.display = "inline-block";
}

For IE7, I want display = "inline".

I've made an attempt at conditional compilation (this showed me how to detect the browser), but it didn't work:

function showHint(myId) 
        {
            document.getElementById(myId).style.display = "inline-block";
            /*@cc_on
                @if(navigator.appVersion.indexOf(“MSIE 7.”)!=-1)
                {
                    document.getElementById(myId).style.display = "inline";
                }
            @*/
        }

Any help is greatly appreciated!

EDIT: I'm not using JQuery.

like image 758
dmr Avatar asked Nov 25 '25 21:11

dmr


2 Answers

Set a global determined by the behavior of IE's conditional comment parsing:

<!--[if IE 7]><script> var isIE7 = true; </script><![endif]-->
like image 82
Jeff Meatball Yang Avatar answered Nov 28 '25 10:11

Jeff Meatball Yang


You need to check navigator.userAgent.

If you use jQuery, you can simply write

if ($.browser.msie && $.browser.version === 7)
like image 34
SLaks Avatar answered Nov 28 '25 10:11

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!