views:

55

answers:

1

Hi!

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!

+1  A: 

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);
}
Q_the_dreadlocked_ninja