2

I need to create a CSS stylesheet class dynamically using JavaScript in IE8.

I have used following code for other browsers:

var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '.cssClass { color: #F00; }';
document.getElementsByTagName('head')[0].appendChild(style);

it's working fine in all browsers except IE8. How to achieve the same in IE8?

Regent
  • 5,142
  • 3
  • 21
  • 35
Akbar Basha
  • 1,168
  • 1
  • 16
  • 38
  • You could simply insert the style to the body of the page with in – Taplar May 07 '15 at 11:17
  • @Taplar i need to apped dynamically – Akbar Basha May 07 '15 at 11:22
  • You can. http://jsfiddle.net/dzv1p8vf/1/ – Taplar May 07 '15 at 11:29
  • This problem has been solved here: http://stackoverflow.com/a/5618889/299756 – kalyfe May 07 '15 at 11:32
  • @taplar that's not working in ie 8 – Akbar Basha May 07 '15 at 11:33
  • @kalyfe i have used css like this var text = ".highlightStyle { fill:" + fill + ";opacity:" + opacity + ";stroke:" + strokeColor + ";stroke-width:" + strokeWidth + "+ }"; fill , opacity, strokeColor and strokeWidth are variable which changed dynamically... those attributes are working fine in all browser except IE8 in IE8 fill , strokeColor,strokeWidth, opacity properties are not working – Akbar Basha May 07 '15 at 11:53

1 Answers1

3

According to MSDN:

The innerHTML property is read-only on the col, colGroup, frameSet, html, head, style, table, tBody, tFoot, tHead, title, and tr objects.

So, try to use innerText to write these class.

Updated:

You can use:

style.styleSheet.cssText = '.cssClass { color: #F00; }';

Or do a test:

if (style.styleSheet){
    style.styleSheet.cssText = '.cssClass { color: #F00; }';
} else {
    style.appendChild(document.createTextNode('.cssClass { color: #F00; }'));
}

Hope, it now runs! :)

Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
  • Ok, wait for a while! – Radonirina Maminiaina May 07 '15 at 11:40
  • @ Radonirina Maminiaina i have used css like this var text = ".highlightStyle { fill:" + fill + ";opacity:" + opacity + ";stroke:" + strokeColor + ";stroke-width:" + strokeWidth + "+ }"; fill , opacity, strokeColor and strokeWidth are variable which changed dynamically... those attributes are working fine in all browser except IE8 in IE8 fill , strokeColor,strokeWidth, opacity properties are not working – Akbar Basha May 07 '15 at 11:57
  • IE8 is a very old browser, but you can see this link: https://css-tricks.com/snippets/css/cross-browser-opacity/ for the opacity – Radonirina Maminiaina May 07 '15 at 12:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77187/discussion-between-radonirina-maminiaina-and-akbar-basha). – Radonirina Maminiaina May 07 '15 at 12:05
  • @ Radonirina Maminiaina sure – Akbar Basha May 07 '15 at 12:06