Adding External Stylesheet using JavaScript
I need to add an link to an external css file in my header using an external javascript file. Don't ask why; I just need to do it. document.writ开发者_Go百科e()
doesn't work btw.
var element = document.createElement("link");
element.setAttribute("rel", "stylesheet");
element.setAttribute("type", "text/css");
element.setAttribute("href", "external.css");
document.getElementsByTagName("head")[0].appendChild(element);
Alternative one liner:
document.head.insertAdjacentHTML( 'beforeend', '<link rel=stylesheet href=/ext.css>' );
精彩评论