Sto sviluppando una pagina Web in cui vorrei aggiungere dinamicamente lo stile da un file javascript esterno.Come aggiungere un tag di stile in una pagina Web da file javascript (js)
Sto usando questo codice che non funziona
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);
}
Utilizzare la formattazione del codice per la domanda. – cederlof