Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding css style from js file crashes IE

Posted on 2009-04-15
4
Medium Priority
?
1,158 Views
Last Modified: 2013-11-18
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

0
Comment
Question by:Dada44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 43

Expert Comment

by:David S.
ID: 24145700
Try using

js4.innerHTML

 instead of

js4.styleSheet.cssText
0
 

Author Comment

by:Dada44
ID: 24145752
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

0
 
LVL 3

Accepted Solution

by:
kurijov earned 2000 total points
ID: 24146077
If you use js framework such as prototype, then this is your code

function loadCSS(css_name, id) {
    var head = $$('head')[0];
    var css = new Element('link', {type: 'text/css', rel: 'stylesheet', href: css_name, media: 'screen'});
    if (id)
        css.id = id;
    else
        css.identify();
    head.insert(css);
}

loadCSS('http://www.myweb.com/css/style.css')

If not, then your code in the snippet. Have a nice day)

function loadCSS(css_name) {
	var head = document.getElementsByTagName('head')[0];
	var css = document.createElement('link');
	css.setAttribute('href', css_name);
	css.setAttribute('type', 'text/css');
	css.setAttribute('media', 'screen, projection');
	css.setAttribute('rel', 'stylesheet');
	head.appendChild(css);
}
 
loadCSS('http://www.myweb.com/css/style.css');

Open in new window

0
 

Author Closing Comment

by:Dada44
ID: 31570311
Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question