Reputation: 5792
I have a button in my page, and onclick event, if should append to the <head>
a CSS file from a server and do something else.
it works perfectly in FF but in IE, it seems not to work (it did append the <link>
to the <head>
- but the CSS won't affect the elements)
Heres my current code:
function loadDynamicCss(filename) {
var fileref = document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
document.getElementsByTagName("head")[0].appendChild(fileref);
}
What can cause this?
Thanks!
Upvotes: 4
Views: 1541
Reputation: 1787
Try this function:
function include_css(url) {
var page = document.getElementsByTagName('head')[0],
cssElem = document.createElement('link');
cssElem.setAttribute('rel', 'css');
cssElem.setAttribute('type', 'text/css');
cssElem.setAttribute('href', url);
page.appendChild(cssElem);
}
Upvotes: 1