...

View Full Version : Adding css file from js file crashes IE



Lleoun
04-15-2009, 09:02 AM
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();

Lleoun
04-15-2009, 10:19 AM
Ok, what about not using import, what about:



<link rel="stylesheet" type="text/css" href="http://www.myweb.com/css/thestyle.css">


I've tried using this:


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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum