I like how Google service pages load up faster than others. Most of it can be attributed to their minimalistic design, but if you ever open up Firebug’s console, you’ll see the actual reason.⌘
The good thing about Javascript is that it’s multi-threaded — as far as asynchronous calls are concerned at least. When you make an XMLHTTPRequest call to external files, it forgets about the process and continues while the content is loaded in the background. I’m going to call out Deepak on this, because even though his method is good, the problem is what he points out himself. Different browsers handle the loading differently. Also, it involves DOM manipulation, which restricts your script to the speed of (X)HTML.⌘
With an asynchronous call, we are bootstrapping our scripts. It allows them to load nicely in the background, while the browser continues to render the markup. One they’re done, they will always be executed in the sequence they are loaded. Gives you at least one constant to work with. If your scripts are large, they will take longer to load. Calling functions that are yet to be loaded is always dangerous, which is why you should use fallback mechanisms.⌘
A call to eval() with the Javascript file’s content passed as a string will result in it being ready for use on the current page. You will have to rewrite your code to JSON so that the evaluation doesn’t throw errors, which will not take too much time. Using the jQuery framework, the code can simply be:⌘
$(document).ready(function(){
$.ajax({
type: "GET",
url: "futureload.js",
data: "ver=1.0",
success: function(content){
eval('js ='+content);
}
});
});
And the corresponding Javascript file could be:⌘
{
alert1: function(){
alert('Greetings from "alert1"');
},
alert2: function(){
alert('Greetings from "alert2"');
}
}
Then call js.function-name() to display the required alert. Using the same skeleton, you can complicate your Javascript file while not losing out on page load times.⌘
Your mileage might vary, and this is definitely the wrong way to make something right. But we’re all for hacks, aren’t we?⌘

