Run Javascript only in IE

Hi,

I have a javascript in my site to make :hover statements work in IE, amongst other IE-specific things.

The script doesn’t really need to run in any other browser, so is there a way of making it run in only IE?
I have tried using <!–[if IE]> in the HTML but this prevents DreamWeaver from updating the link to the .js file, in pages based on my template.

Is there anything I can do inside the java to detect the browser, and run the script if the browser is IE, and otherwise not run it?

Thanks,

Rich.

<!–[if IE]>
<script type=“text/javascript” src="ieonly.js></script>
<![endif]–>

Hi,

Thanks for your suggestion - I’m sure there’s a way of detecting a browser without going into what it can/cannot do though.

I found an article on Quirksmode about how to do it, and I’ve thrown this together, although I’m not sure how well it works, as I’m a beginner at Java:

var detect = navigator.userAgent.toLowerCase();
var browser,total,thestring;

if (checkIt('msie'))
	{
		browser = "ie";
	}
else browser = "other";

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;

	if (browser = "ie") {

//JAVA FOR FIXING IE PROBLEMS GOES IN HERE

	}
	else if (browser = "other") {
	
	}
}
window.onload = checkIt;

Can anything be made of this?

Edit: a link to the QuirksMode article that I mentioned:
http://www.quirksmode.org/js/detect.html

Thanks,

Rich.

Hi

What is the most ACCURATE but LIGHTWEIGHT and SIMPLE way of determining whether a user has Microsoft Internet Explorer?

Generally, you shouldn’t care what a browser’s name is. All you should care about is whether the browser supports the property or function you want to use. So, you do this:


if(document.getElementbyId)
{
    var myDiv = document.getElementById("div1");
}

That way, the code will execute in any browser that supports the document.getElementById() function, not just IE.

The code I posted above is the ONLY way of guaranteeing that the browser is IE. Many other browsers pretend to be IE to get past ALL of the other ways of testing for it.

What felgall has suggested is called ‘conditional comments’ and they are ignored by everything except IE 5 or later on Windows (or Wine/Crossover Office). They can be useful for isolating scripts and CSS rules for IE. You can learn more at: http://msdn.microsoft.com/workshop/author/dhtml/overview/ccomment_ovw.asp