Saturday, 7. March 2009, 00:01:03
For a couple of days in November, the
New York Times looked serenely white in Opera:

You might think that nothing newsworthy was happening on the planet. Unfortunately it was not the start of a new and peaceful world order, it was merely their JavaScript playing
timing roulette:
if (typeof callback == 'function') {
if (document.addEventListener) {
window.setTimeout(function(){
document.write('<script type="text/javascript" charset="utf-8">(' + callback.toString() + ')();<\/script>');
}, 0)
}
What is that doing? This code says "have a 0 millisecond break, then add this SCRIPT tag to the document". (By "0 millisecond" it sort of means "as soon as you get around to it".) The problem with that is that
if the browser happens to complete loading the document before running that document.write() statement it will
replace the current document with a single, invisible SCRIPT tag.
Opera, doing its best at trying to show you the page as soon as possible
did finish loading the document before getting back to the timeout - so the page was overwritten and disappeared.
Now, by the above it sounds like it is a drawback to be fast (
again?). Wait until you see an extract of some code that broke image upload on
Orkut:
<head>
<script>setTimeout( function(){ document.body.appendChild(el) }, 0 )</script>
<script>/*other stuff here*/</script>
</head><body>
In this case, if the timeout runs before we've seen the BODY tag - it will break, because no document.body exists yet. Wait, at New York Times parsing quickly was a
mistake and here..we're punished because we
didn't get to BODY yet when running the timeout?? So we're simultaneously too fast for NYTimes and too slow for Orkut..
This is JavaScript timing roulette - such things happen when websites are written according to the timing of specific browsers, or even the speed of the network connection the web site developer uses! What would happen if you're visiting Orkut on a really slow connection and the network has a small hiccup-pause between HEAD and BODY? By Orkut's code I would not be surprised if certain network delays - only a few milliseconds - would break the site entirely.
And it gets worse.
AOL sites often try to launch slideshows in popup windows. In Opera that doesn't go very well:
AOL is another gambler addicted to playing the timing roulette. Bear with me, because this gets complex - but they have something like
<SCRIPT src="http://www.aolcdn.com/ke/swfobject/ke_kit_popup_includes.js" type="text/javascript" language="javascript" charset="utf-8"></SCRIPT>
<DIV id="gallery-holder">
<DIV id="news-news_popup_foobar">
The popup_includes.js contains code that appends external scripts to HEAD. The final external scripts (ke_kit_refresh.js) contains further inline code that calls rederPopupPage() which calls a method in the opener window that eventually calls the embedswf() method which starts looking for the element with the ID "news-news_popup_foobar" (where 'foobar' is actually the short name for the article).
Since loading scripts block parsing, we have not yet reached the DIV when the slideshow is supposed to be inserted. So when the script asks "do you have the element news-news_popup_foobar?" Opera responds "no, not yet anyway" and the script just gives up.
So, apparently we're supposed to keep parsing forward while we're running scripts dynamically added earlier in the DOM?! The
W3C never told us that, I think. It's the web's
dark matter striking again.