Getting IE to load a different js file

北战南征 提交于 2020-01-04 07:19:22

问题


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>

回答1:


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>



回答2:


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);



回答3:


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:


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>



回答5:


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>


来源:https://stackoverflow.com/questions/17125660/getting-ie-to-load-a-different-js-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!