4

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!

3
  • Are you trying to reload the same stylesheet? Commented Aug 20, 2010 at 12:21
  • Is this a simple IE caching issue? Have you tried pasting the url from the appended link tag into IE's URL field, then reloading the page? Commented Aug 20, 2010 at 12:57
  • Possible duplicate of stackoverflow.com/questions/1184950/… Commented Aug 29, 2012 at 6:38

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't work for me when views are rendered post factum with ejs.
@Himberjack: Can you confirm that this solution worked for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.