Preload script file

Mattia picture Mattia · Aug 27, 2010 · Viewed 11.4k times · Source

There are numerous plug-ins to preload images, but is there a way to preload javascript? My application uses a big js file and it take about 5 seconds or so to load before the page shows... so, is there a way I can display a "loading message" while I somehow preload the script? (A sort of 'Loading...' like in Gmail)

Thanks

Answer

RussellUresti picture RussellUresti · Aug 27, 2010
$.getScript('script.js',function(){
  //this is your callback function to execute after the script loads
});

getScript should work fine. While the script is loading (I assume on an action or something?) show the loading message and call getScript, then in the callback area, change the text that says "loading" to "done" and execute whatever you want.