Internet Explorer 9 has a bug which means it can't process more than 31 top-level stylesheets. (This actually affects 7 and 8 as well, but people are really good about updating Internet Explorer, aren't they? What's that? Oh)
The problem comes from the way IE stores CSS rules. Each rule is referenced as a 32 bit number. The first 5 bits are the ID of the top level stylesheet the rule came from, and the last 12 bits are the rule. The 15 bits in the middle are @import references. You can chain @imports three levels down (so 5 bits each again), but only from those first 31 stylesheets.
Now, it might seem quite odd to want to link 31 stylesheets, but given the way sites are built up from 3rd party code it's not that uncommon. Where I work now we provide just such a third party service (advertising) and our styles were getting ignored by the browser because there were more than 31 stylesheets above ours (we load all our stuff on DOMContentLoaded from a Javascript tag, so there's not much chance to fix the load order.)
I came up with a (what I think is a) neat fix for this. We simply copy the CSS into a JavaScript file as a string and load that, then append the that string to the cssText property of one the existing stylesheets in the page like so:
document.styleSheets[0].cssText += ourCss;
The browser now processes the CSS, without busting its 31 stylesheet limit. You can't use this to insert an @import however as that isn't strictly cssText, it doesn't get reprocessed.
Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts
Friday, June 22, 2012
Wednesday, March 21, 2012
Overriding core JavaScript methods for debugging, fun, profit
Hallvord Steen's epic tale of debugging a Google Maps bug is essential reading for anyone dealing with JS on a daily basis. It's full of useful techniques to adapt and use.
What I especially like is his trick for catching bugs associated with core JavaScript functions, when you don't know how the code is called: just redefine the function to something that doesn't work.
What I especially like is his trick for catching bugs associated with core JavaScript functions, when you don't know how the code is called: just redefine the function to something that doesn't work.
javascript:void(Element.prototype.removeChild=function(){undefined()})
When the script needs to call removeChild it uses the overridden function and (because undefined() doesn't really exist) throws a handy stack trace into the console so you can see where the problem occurs.
Subscribe to:
Posts (Atom)