Reputation: 25
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>
Upvotes: 1
Views: 5260
Reputation: 23396
The problem here is the closing script
tag. When using document.write()
to add scripts, you need to cut end tag into pieces. Something like below (notice the fixed quoting and parenthesing too).
document.write('<script src="js/moreskins.js" type="text/javascript"></scr' + 'ipt>');
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.
Upvotes: 3
Reputation: 15714
Thought I'd take a stab at writing some code too ;-)
<script>
(function(){
var script = document.createElement("script"),
is_ie = (/MSIE/gi.test(navigator.userAgent));
script.src = (is_ie) ? 'js/ieskins.js' : 'js/moreskins.js';
document.getElementsByTagName('head')[0].appendChild(script);
}());
</script>
Upvotes: 0
Reputation: 1000
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":
<script type="text/javascript">
var ie = !(navigator.userAgent.indexOf("MSIE")<0);
if(ie == false) {
document.write ("<script src=\"js/moreskins.js\"></scr"+"ipt>");
} else {
document.write ("<script src=\"js/ieskins.js\"></scr"+"ipt>");
}
</script>
Upvotes: 4
Reputation: 722
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]>
<script type="text/javascript">
var ie = false;
if(/MSIE/gi.test(navigator.userAgent)){
ie = true;
}
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>
Upvotes: -1
Reputation: 7442
Use conditional comments to load file in IE and ignore in other browsers.
<!--[if IE]>
<script src="js/ieskins.js" type="text/javascript">
<![endif]-->
<![if !IE]>
<script src="js/moreskins.js" type="text/javascript">
<![endif]>
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
var script = document.createElement('script');
script.src = "js/moreskins.js";
document.getElementsByTagName('head')[0].appendChild(script);
Upvotes: 3