Getting IE to load a different js file

2019-07-15 09:23发布

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>

5条回答
三岁会撩人
2楼-- · 2019-07-15 09:26

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>
查看更多
仙女界的扛把子
3楼-- · 2019-07-15 09:27

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.

查看更多
狗以群分
4楼-- · 2019-07-15 09:30

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>
查看更多
Juvenile、少年°
5楼-- · 2019-07-15 09:42

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>
查看更多
混吃等死
6楼-- · 2019-07-15 09:48

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);
查看更多
登录 后发表回答