views:

48

answers:

5

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.

A: 

There is a Jquery Plugin detecting the Browser Version. I do not know the exact Link ...

Paul Weber
+2  A: 

You need to check navigator.userAgent.

If you use jQuery, you can simply write

if ($.browser.msie && $.browser.version === 7)
SLaks
+6  A: 

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

<!--[if IE 7]><script> var isIE7 = true; </script><![endif]-->
Jeff Meatball Yang
Thank you so much! :)
dmr
+1  A: 

I think you can use regular expression to determine MSIE 7:

if(/MSIE 7/.test(navigator.appVersion)) { /* msie7 related code */ }
Andy
A: 

Conditional compilation should work. It seems that your error is that you are using fancy quotes to delimit a string (“MSIE 7.”) or that you are attempting to assign display to something unknown to IE and it is having a fit over it and throwing an error. Here is a more concise version of the function that doesn't repeat itself and solves this issue:

function showHint(myId) 
{
    document.getElementById(myId).style.display = "inline" //@cc_on;
    + "-block";
}
Eli Grey