Trying to get a page to load a different js file if the browser is IE, but a different one if it is any other browser. I've corrupted this, but it won't work, does anyone have any ideas?
Any help is appreciated:
<script type="text/javascript">
var ie = false;
</script>
<!--[if IE]>
<script type="text/javascript">
ie = true;
</script>
<![endif]-->
<script type="text/javascript">
if(ie == false)
{
document.write ("<script src="js/moreskins.js" type="text/javascript">")</script>;
} else {
document.write ("<script src="js/ieskins.js" type="text/javascript">")</script>;
}
</script>
Thought I'd take a stab at writing some code too ;-)
The problem here is the closing
script
tag. When usingdocument.write()
to add scripts, you need to cut end tag into pieces. Something like below (notice the fixed quoting and parenthesing too).Script execution is stopped, when parser founds the first literal end tag, that's why you need to cut it in the argument.
Also notice, that IE10 doesn't support conditional comments, you should rather use feature detection instead of browser detection.
Looks like your last script tag is a little jumbled, plus as Teemu mentioned, IE10 does not support conditional HTML comments.
If you really need to target IE, I would check the user agent for "MSIE":
Using navigator.userAgent is better and simple. You code doesn't work in any else browser except IE,because only IE understand the mean of
<!--[if IE]>
Use conditional comments to load file in IE and ignore in other browsers.
Note: the else part (
<![if !IE]>
) which is not a comment. So for IE it is else part and for other browsers, it is nothing.EDIT
you can also try the following instead of
document.write