Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding css file from js file crashes IE

    Hi all,

    From a js file I want to add the following to the head tag:

    Code:
    <style type="text/css">
     @import "http://www.myweb.com/css/style.css";
    </style>
    I'm using the code below, and it works perfect for Firefox and the rest, but it crashes Internet Explorer.
    Can someone please give me a hand with it??

    Thanks a ton in advance!

    Code:
    function newScript4(){
    var js4=document.createElement('style');
    js4.setAttribute("type","text/css");
    var thetext = "@import 'http://www.myweb.com/css/style.css';";
    
    if (js4.styleSheet) {   // IE
        js4.styleSheet.cssText = thetext; // this line crashes IE
        js4.styleSheet.addImport('http://www.myweb.com/css/style.css'); //this other line cashes IE too
    } else {                // the world
        var tx = document.createTextNode(thetext);
        js4.appendChild(tx);
    }
    
    document.getElementsByTagName('head')[0].appendChild(js4)
    }
    
    newScript4();

  • #2
    New Coder
    Join Date
    Feb 2008
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, what about not using import, what about:

    Code:
    <link rel="stylesheet" type="text/css" href="http://www.myweb.com/css/thestyle.css">
    I've tried using this:
    Code:
    function newScript4(thestyle){
    var js4=document.createElement('link');
    js4.type='text/css';
    js4.rel='stylesheet';
    js4.href=thestyle;
    document.getElementsByTagName('head')[0].appendChild(js4)
    }
    newScript4('http://www.myweb.com/css/thestyle.css');
    And it does not crash, but I cannot see it in the source code. This is for Internet Explorer in Firefox shows and works.

    Please help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •