Getting around the blocking of script
Date : 2008 07 24 Category : Tech & DevelopmentStoyan Stefanov has a post that discusses the issues with browsers blocking on script tags.
He discusses the general problem and how YUI has a helper for it by using onreadystatechange and onload:
PLAIN TEXT JAVASCRIPT: var myHandler = { onSuccess: function(){ alert(':))'); }, onFailure: function(){ alert(':(('); } }; var urls = ['1.js', '2.js', '3.js']; YAHOO.util.Get.script(urls, myHandler);The game is changing though, as he mentions:
Safari and IE8 are already changing the way scripts are getting loaded. Their idea is to download the scripts in parallel, but execute them in the sequence they’re found on the page. It’s likely that one day this blocking problem will become negligible, because only a few users will be using IE7 or lower and FF3 or lower. Until then, a dynamic script tag is an easy way around the problem.
Danny Thorpe wrote some comments and the issue with Safari:
This works great for Firefox and IE, but fails completely in the Safari browser. Safari doesn’t implement onLoad notifications for script tags. This forces you to abandon the onLoad technique and instead use the technique of embedding something at the end of each script file to signal when the file has been loaded.
The script libraries for the Windows Live sites use this technique - every source file contains a function call at the bottom that tells a central notifier that it has been loaded. Other code can ask the notifier to signal them when a file or set of files have been loaded. This works in all browsers without relying on diverging browser idiosyncrasies.