43

I have an Ajax control that is loaded into a Yahoo popup using jQuery.

I just use a simple .get request to load the HTML.

  $.get(contentUrl, null, function(response) {
         $('#dialog').find('.bd').assertOne().html(response);
     }, "waitDlg");

Now the problem is that the content that is loaded needs its own CSS which is actually dynamically created. I have a choice of either inlining the or using an external CSS stylesheet.

Testing in Chrome shows that the CSS loaded via AJAX is not evaluated/applied at the time it is added to the DOM using the above code.

Internet Explorer will evaluate an inlined CSS when it just gets stuck in the DOM but Chrome will not. I am currently unable to test in FireFox because of a completely unrelated issue.

Is there any way in jQuery to evaluate a stylesheet that was dynamically added to the DOM as either an inline or ?

There are many reasons I'd like to do this:

  • the CSS in the popup belongs to the popup and may be coming from a different environment altogether
  • it is dynamic and I don't want to put it in the parent page unless I absolutely have to
  • I planned for it to work like this and it doesn't! :-(

3 Answers 3

79

Given a path to your stylesheet (or some URL that will generate valid CSS):

var myStylesLocation = "myStyles.css";

...either one of these should work:

Load using AJAX

$.get(myStylesLocation, function(css)
{
   $('<style type="text/css"></style>')
      .html(css)
      .appendTo("head");
});   

Load using dynamically-created <link>

$('<link rel="stylesheet" type="text/css" href="'+myStylesLocation+'" >')
   .appendTo("head");

Load using dynamically-created <style>

$('<style type="text/css"></style>')
    .html('@import url("' + myStylesLocation + '")')
    .appendTo("head");

or

$('<style type="text/css">@import url("' + myStylesLocation + '")</style>')
    .appendTo("head");
Sign up to request clarification or add additional context in comments.

8 Comments

this looks great. AFAYK are these all 'supported' ways of doing this or is this at all hacky? it looks very promising but i'm worried about the one browser that won't let me do this.
They're all valid, but the only way to know for sure if a given browser reacts to the changing DOM correctly is to try it. The last two (thanks, gs!) come with an additional caveat: if your server is misconfigured and sends your CSS down with the wrong MIME type, Firefox will ignore it unless your page is in quirks mode.
works great : $('<link rel="stylesheet" type="text/css" href="'+myStylesLocation+'" >') .appendTo("head");
may not work on IE7. have not verified yet - but please see answer from @user406905
Would be cool if StackOverflow has a little popup testing matrix with all the browsers and versions and we could mark which combinations worked for each answer. A bit like this matrix.cpantesters.org/?dist=WWW-Mechanize+1.72
|
27

The accepted answer will not work in IE 7 (and buggy in IE 8). the following will work in IE as well as FF and chrome/safari:

var url = 'urlOfStyleSheet.css'
if(document.createStyleSheet) {
    try { document.createStyleSheet(url); } catch (e) { }
}
else {
    var css;
    css         = document.createElement('link');
    css.rel     = 'stylesheet';
    css.type    = 'text/css';
    css.media   = "all";
    css.href    = url;
    document.getElementsByTagName("head")[0].appendChild(css);
}

3 Comments

thanks! now if i could only remember where i used this so i could go change it
this works in ie8 but not ie7. The accepted answer does not work in either.
this works for IE 8 document.createStyleSheet and yes: the other technics seem very buggy with IE 8. They can work, but the bigger the page / the slower the load, it seemed to stop working. document.createStyleSheetworked great so far for me
1
var cssPath = "/path/to/css/";

var linkStr = document.createElement("&lt;link rel='stylesheet' type='text/css' href='"+cssPath+"' media='screen' /&gt;");

document.getElementsByTagName("head")[0].appendChild(linkStr);

1 Comment

Sorry, meant to add this as a comment to Cris Rus' question.

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.