tags:

views:

138

answers:

2

I have some jQuery that loads a new stylesheet like so

$('head').append('<link rel="stylesheet" type="text/css" href="' + config.basePath + config.stylesheetsPath + 'sections/' + name + '.css" media="screen" class="new site-specific" />');

I am then attempting to get some info from that CSS file immediately after. It generally will not display the correct information until the 2nd trigger of the event (after I imagine the stylesheet has been loaded).

Is there any event I can attach to trigger when the CSS is loaded?

Thanks

A: 

I don't know about triggering an event, but can you make an get request for the CSS file and then just update the <head> with a new <style> tag, placing the content of the file inside the <style> tag?

$(document).ready(function() { 
   $.get("http://example.com/style.css", function(data) {
      $('head').append("<style>" + data + "</style>");
   });
   ... /* your code */ ...
});
beggs
This works, except the image paths are pointing to different areas once they are included like this.
alex
That may be because the relative paths used in the CSS file are not valid from the HTML file. You can use absolute paths or update the relative paths as though the css file was in the same directory as the HTML page.
beggs
A: 

Unless there is some kind of "onload" event for the CSS (and there should be, as firebug will show such a thing), I would use the following procedure:
1. Put a hidden DOM element with color X.
2. in the uploaded CSS this color will be changed to y
3. Once you initiate the upload (in your JS) you create a listner that will rise what ever function you want when the color changes to y.

By listner I mean use setInterval.

Itay Moav