Skip to main content
properly formatted code, fixed grammar and layout.
Source Link
Peter Kelly
  • 14.4k
  • 7
  • 56
  • 64

I am developing a web page in which I would like to add style tag dynamically from an external javascript file.

I am using this code

this.addStyle = function() { if (! this.inited) { alert("not inited"); return; } var head = document.getElementsByTagName('head')[0], style = document.createElement('style'), rules = document.createTextNode('.bodys { bgcolor="red"; }'); style.type = 'text/css'; if(style.styleSheet) style.styleSheet.cssText = rules.nodeValue; else style.appendChild(rules); head.appendChild(style); } but it doesn't that does not work

please answer me some possible solution.

Thanks!

this.addStyle = function()
    {
        if (! this.inited) {
          alert("not inited");
          return;
         }
        var head = document.getElementsByTagName('head')[0],
        style = document.createElement('style'),
        rules = document.createTextNode('.bodys { bgcolor="red"; }');
        style.type = 'text/css';
        if(style.styleSheet)
            style.styleSheet.cssText = rules.nodeValue;
        else style.appendChild(rules);
            head.appendChild(style);
    }

I am developing a web page in which I would like to add style tag dynamically from an external javascript file.

I am using this code

this.addStyle = function() { if (! this.inited) { alert("not inited"); return; } var head = document.getElementsByTagName('head')[0], style = document.createElement('style'), rules = document.createTextNode('.bodys { bgcolor="red"; }'); style.type = 'text/css'; if(style.styleSheet) style.styleSheet.cssText = rules.nodeValue; else style.appendChild(rules); head.appendChild(style); } but it doesn't work

please answer me some possible solution.

Thanks!

I am developing a web page in which I would like to add style tag dynamically from an external javascript file.

I am using this code that does not work

this.addStyle = function()
    {
        if (! this.inited) {
          alert("not inited");
          return;
         }
        var head = document.getElementsByTagName('head')[0],
        style = document.createElement('style'),
        rules = document.createTextNode('.bodys { bgcolor="red"; }');
        style.type = 'text/css';
        if(style.styleSheet)
            style.styleSheet.cssText = rules.nodeValue;
        else style.appendChild(rules);
            head.appendChild(style);
    }
Source Link
Tokendra Kumar Sahu
  • 3.5k
  • 11
  • 30
  • 30

How to add a style tag in a web page from external javascript(js) file

I am developing a web page in which I would like to add style tag dynamically from an external javascript file.

I am using this code

this.addStyle = function() { if (! this.inited) { alert("not inited"); return; } var head = document.getElementsByTagName('head')[0], style = document.createElement('style'), rules = document.createTextNode('.bodys { bgcolor="red"; }'); style.type = 'text/css'; if(style.styleSheet) style.styleSheet.cssText = rules.nodeValue; else style.appendChild(rules); head.appendChild(style); } but it doesn't work

please answer me some possible solution.

Thanks!