views:

200

answers:

4

It works fine in Firefox and Chrome, but does not work in IE8. Here is the html structure:

<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
  <script type="text/javascript">
   $(function() {
    // this does not work in the stupid IE
    $('<style type="text/css"></style>').text('body {margin: 0;}').appendTo('head');
   });
  </script>
 </head>
 <body>
 </body>
</html>

And what' s the alternative to do this in IE?

A: 

Try the css function in jquery

$( 'body' ).css( { margin: 0 } )
hookedonwinter
what if I need to append a lot of CSS rules? For example, I want to inject a whole CSS file into the HTML?
powerboy
why don't you just insert it?<link href="file.css" type="text/css" rel="stylesheet" />
Jeremy B.
Because I want to insert some CSS rules on demand. In some cases the webpage does not need those rules if a user does not trigger an event. Attached an extra CSS file require another HTTP request.
powerboy
@powerboy I could see that being a problem if you have a lot of things to add. You could do it all my way, but it would be a lot of find and replace! How many rules do you have?
hookedonwinter
I see, reference my answer to see how to insert a CSS file on demand.
Jeremy B.
A: 

To dynamically load a CSS file using JQuery.

var link = $("<link>");
link.attr({
    type: 'text/css',
    rel: 'stylesheet',
    href: <your CSS file url>
});
$("head").append( link ); 
Jeremy B.
`$('<div>hello world</div>').appendTo($('body'));` will create a new div and append it to the body. It's not a selector.
Ken Browning
@Jeremy B - You solution is inserting a CSS file dynamically. But what I am looking for is an easy way to insert several (but not only one) CSS rules (not a CSS file) dynamically, since attaching another CSS file initializes another HTTP request. My program needs to run very fast and the server can be quite busy sometimes. I need to do everything to reduce the response time.
powerboy
I'm not sure how long you think the http request will take, but as far as it goes, I don't believe loading all the CSS rules, and then NOT using them will be faster than getting the CSS file you'll need, unless you are looking at just a few CSS rules. How much CSS are we talking about?
Jeremy B.
Those CSS rules can be written in JS in advance, so they are transmitted in the same HTTP request with the JS file. I just want those CSS rules to apply to the webpage on demand.
powerboy
+5  A: 

This is working for me in IE7:

$('<style type="text/css">body {margin: 0;}</style>').appendTo($('head'));

Another syntax which might be easier to read:

$('head').append('<style type="text/css">body {margin:0;}</style>');

However, calling either .text(val) or .html(val) to set the contents of the style tag will cause an exception to be thrown because they set the innerHTML DOM property which is read-only.

Here is IE's documentation of the innerHTML property:

The property is read/write for all objects except the following, for which it is read-only: COL, COLGROUP, FRAMESET, HEAD, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR.

Ken Browning
I'm having the same issue with text and html in IE8.
drs9222
@Ken Browning - Yes, I have already tried the same thing as your solution. It does work in IE8 and compatibility view. But I just think that <code>var rules = '100 letters here'; $('<style type="text/css"></style>').text(rules).appendTo('head'); </code> looks prettier than <code>var rules = '100 letters here'; $('<style type="text/css">' + rules + '</style>').appendTo('head'); </code>
powerboy
You can choose pretty or working. Up to you. IE seems to think `innerHTML` should be read-only for `style` elements.
Ken Browning
I'm using JQuery 1.4.1 with IE8 in standards mode and $('<style></style).text('...') will not work but $('<span></span).text('...'). I can't even append to a style element created like this.
drs9222
"IE seems to think innerHTML should be read-only for style elements". Maybe that is the point!
powerboy
A digression: anybady know if there is a comprehensive collection of cross-browser issues (especially for the stupid IE) on the web? CSS hacks, JS hacks, etc. A comprehensive one!
powerboy
+1 Good find Ken
Josh Stodola
A: 

Personally I would do this.

var styles = {
  margin: '0',
  padding: '5px',
  border: 'solid 0px black'
}

$('body').css(styles);
James Westgate
ahh...this solution looks pretty! But if we need to apply multiple rules to different DOM elements, it will become wordy.
powerboy