0

Trying to get my site running in IE6 but having trouble with some runtime css stuff that I need to do.

I've tried both these:

$.rule('#post'+ i + '{ visibility:hidden;}').appendTo('style');

-

var postStyle = document.createElement('style');
postStyle.type = 'text/css';    
postStyle.innerHTML = '#post'+ i + '{ visibility:hidden;}';
document.getElementsByTagName('head'[0].appendChild(postStyle);

Works in other browsers but not IE. I get this error:

"Object doesn't support this property or method"

Any ideas?

Joe Hamilton
  • 665
  • 2
  • 9
  • 19

1 Answers1

0

In IE, you have to do this:

postStyle.styleSheet.cssText = whatever;

I've done this generally with a try/catch setup.

$('head').append($('<style/>', { id: "replaced-colors" }));

try {
  $('#replaced-colors').html(replaced);
}
catch (_ie) {
  $('#replaced-colors')[0].styleSheet.cssText = replaced;
}

as a real-life example.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 1
    You can avoid the `try/catch` by adding a rule to an existing stylesheet obtained from the `document.styleSheets` collection and checking for the existence of its `addRule()` method. – Tim Down Jun 11 '12 at 13:28