I'm running the following code in jQuery to dynamically load a stylesheet. Its purpose is to allow users to load a custom stylesheet and change the look of a JavaScript widget:
this.loadStyleSheet = function(url){
$("<link rel='stylesheet' type='text/css' />").attr("href", url).appendTo("head");
}
For a split second the screen appears jumpy, since the correct style is applied to the widget only after the dynamic stylesheet is loaded. To solve this issue, I need to know when in fact the dynamic stylsheet has finished loading.
Although this issue has been discussed before, none of the solutions apply in my case:
- .load() only fires in IE, not in Firefox. A cross browser solution is required. BTW, .ready() fires off before the stylesheet has finished loading.
- .get() only works on the same domain. The stylesheet in my case may come from any given domain.
- Can't query for some CSS property to change via setTimeout. The user may point to any custom stylesheet, and there is no way to know what her CSS file will contain.
- Waiting for a constant time after loading the stylsheet via setTimeout is obviously a bad solution. There is no way to know in advance how long it will take the stylesheet to load, if it will load at all.
Any new ideas regarding this issue? Your help is much appreciated.