admin管理员组文章数量:1414621
I'm trying to dynamically add a script if a user is using Internet explorer. I tried IE Conditionals but chrome didn't like the !IE
tag so I'm trying it the jquery way:
<script type="text/javascript" src=".7.2/jquery.min.js" ></script>
<script type="text/javascript">
if($.browser.msie){
document.write('<script type="text/javascript" src="ie/ieFix.js" ></script>');
}
else{
document.write('<script type="text/javascript" src="dynamic.js" ></script>');
}
</script>
I'm not sure why its not letting me just add this, instead it returns '); } else{ document.write(''); }
Throwing them in variables has the same effect :/
IE Conditionals
<!--[if !ie]>-->
<script type="text/javascript" src="dynamic.js" ></script>
<!--<![endif]-->
<!--[if ie]>-->
<script type="text/javascript" src="ie/ieFix.js" ></script>
<!--<![endif]-->
I'm trying to dynamically add a script if a user is using Internet explorer. I tried IE Conditionals but chrome didn't like the !IE
tag so I'm trying it the jquery way:
<script type="text/javascript" src="https://ajax.googleapis./ajax/libs/jquery/1.7.2/jquery.min.js" ></script>
<script type="text/javascript">
if($.browser.msie){
document.write('<script type="text/javascript" src="ie/ieFix.js" ></script>');
}
else{
document.write('<script type="text/javascript" src="dynamic.js" ></script>');
}
</script>
I'm not sure why its not letting me just add this, instead it returns '); } else{ document.write(''); }
Throwing them in variables has the same effect :/
IE Conditionals
<!--[if !ie]>-->
<script type="text/javascript" src="dynamic.js" ></script>
<!--<![endif]-->
<!--[if ie]>-->
<script type="text/javascript" src="ie/ieFix.js" ></script>
<!--<![endif]-->
Share
Improve this question
edited May 2, 2012 at 23:13
Howdy_McGee
asked May 2, 2012 at 22:55
Howdy_McGeeHowdy_McGee
10.7k30 gold badges116 silver badges191 bronze badges
3 Answers
Reset to default 4The </script>
in the line
document.write('<script type="text/javascript" src="ie/ieFix.js" ></script>');
actually matches as the end tag of
<script type="text/javascript">
on the line above it. You should rewrite it to something like
document.write('<script type="text/javascript" src="ie/ieFix.js" ></scr' + 'ipt>');
so you don't confuse the browser with the closing </script>
tag.
Any IE check
function detectIEEdge() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
Chrome works fine with !IE
if you do it right:
<!--[if !ie]>-->
Put stuff for non-IE browsers here
<!--<![endif]-->
The reason you are having problems in your code above is because the HTML parser sees the </script>
as the end of the script rather than a part of the string. People usually fix this by using <\/script>
.
Update: You're doing your "if IE" wrong now.
<!--[if ie]>
IE stuff - this is a "conditional reveal"
<![endif]-->
<!--[if !ie]>-->
Non-IE stuff - this is a "conditional hide"
<!--<![endif]-->
本文标签: javascriptIf Internet ExplorerAdd ScriptStack Overflow
版权声明:本文标题:javascript - If Internet Explorer - Add Script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745173613a2646123.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论