Link to home
Start Free TrialLog in
Avatar of Dada44
Dada44Flag for Spain

asked on

Adding css style from js file crashes IE

Hi all,

From a js file I want to add the following to the head tag:
<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!

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

Open in new window

Avatar of David S.
David S.
Flag of United States of America image

Try using

js4.innerHTML

 instead of

js4.styleSheet.cssText
Avatar of Dada44

ASKER

Kravimir, thanks a lot for answering.

Following your suggestion I'm using the code below.
It does not crash Internet Explorer, but I get a "unknown error in execution time" error and I cannot see the style imported in the source code :(
Did I get your suggestion correctly?
Thanks again!
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.innerHTML = 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();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kurijov
kurijov
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dada44

ASKER

Thanks!