I have some code (it's actually not mine but the SlickGrid library) that creates a <style>
element, inserts it into the DOM, then immediately tries to find the new stylesheet in the document.styleSheets collection.
In WebKit this sometimes fails. I don't actually have any idea what the circumstances are, but it's nothing that's consistently reproducible. I figured I could work around it by changing the code so the check for the StyleSheet object doesn't happen until the load
event on the style element, like so:
$style = $("<style type='text/css' rel='stylesheet' />").appendTo($("head"));
var rules = ...;// code to create the text of the rules here
if ($style[0].styleSheet) { // IE
$style[0].styleSheet.cssText = rules.join(" ");
} else {
$style[0].appendChild(document.createTextNode(rules.join(" ")));
}
$style.bind('load', function() {
functionThatExpectsTheStylesheet();
});
and functionThatExpectsTheStylesheet attempts to locate the actual stylesheet object like so:
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if ((sheets[i].ownerNode || sheets[i].owningElement) == $style[0]) {
stylesheet = sheets[i];
break;
}
}
but sometimes even at that point, the stylesheet object is not found.
So, my question is this:
- Does the
load
event in fact not guarantee that the styleSheet object will be available? Is it a bug? - If not, is there another condition that I can use or do I just need to do this using timeouts?
- Or is there some problem with the way I'm binding the load event and that's my problem?