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
Add a ment  | 

3 Answers 3

Reset to default 4

The </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