0

So I'm trying to load a different css stylesheet if IE is detected.

My code:

<head>
<!-- other scripts and stylesheets -->

<!--[if IE]>
  <link href='css/index_ie.css' rel='stylesheet' type='text/css'>
<![endif]-->
<!--[if !IE]>
  <link href='css/index.css' rel='stylesheet' type='text/css'>
<![endif]-->

<script>
(function() {
    if (typeof ActiveXObject === "undefined") {
        var s = document.createElement('link');
        s.href = "css/index.css";
        s.rel = "stylesheet";
        s.type = "type/css";
        document.documentElement.appendChild(s);
    }
    else {
        var s = document.createElement('link');
        s.href = "css/index_ie.css";
        s.rel = "stylesheet";
        s.type = "type/css";
        document.documentElement.appendChild(s);
    }
})();
</script>
</head>

This loads index.css correctly when not-IE browser. However this is not loading index_ie.css when IE is being used.

PS: testing with IE 10

3
  • for IE use this var s = document.createElement('<link href="css/index_ie.css" rel="stylesheet" tye="text/css"/>'); Commented Jul 23, 2013 at 10:36
  • Also, append it in the head element. document.getElementsByTagName("head")[0].appendChild(s). Commented Jul 23, 2013 at 10:38
  • Tried that, didn't work. Commented Jul 23, 2013 at 10:40

1 Answer 1

1

To include content in browsers other than IE 9 and older, use a slightly different syntax:

<![if IE]>
  <link href='css/index.css' rel='stylesheet' type='text/css'>
<![endif]>

As it explains:

The downlevel-revealed conditional comment enables you to include content in browsers that don't recognize conditional comments. Although the conditional comment itself is ignored, the HTML content inside it is not.

Though, note that conditional comments aren't fully supported after IE 9 (from the top of the same page):

Important As of Internet Explorer 10, conditional comments are no longer supported by standards mode. Use feature detection to provide effective fallback strategies for website features that aren't supported by the browser. For more info about standards mode, see Defining Document Compatibility.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.