Reputation: 22333
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.
Upvotes: 2
Views: 293
Reputation: 35850
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";
}
Upvotes: 0
Reputation: 12230
I think you can use regular expression to determine MSIE 7:
if(/MSIE 7/.test(navigator.appVersion)) { /* msie7 related code */ }
Upvotes: 1
Reputation: 39017
Set a global determined by the behavior of IE's conditional comment parsing:
<!--[if IE 7]><script> var isIE7 = true; </script><![endif]-->
Upvotes: 7
Reputation: 887415
You need to check navigator.userAgent
.
If you use jQuery, you can simply write
if ($.browser.msie && $.browser.version === 7)
Upvotes: 2
Reputation: 6688
There is a Jquery Plugin detecting the Browser Version. I do not know the exact Link ...
Upvotes: 0