views:

25

answers:

2

I'm using the following code to allow parallel JavaScript downloading on my website

var head  = document.getElementsByTagName("head")[0]; 

var sTag1 = document.createElement("script");
sTag1.type = sTag1.type = "text/javascript";
sTag1.src = "http://example.com/one.js";

var sTag2 = document.createElement("script");
sTag2.type = sTag2.type = "text/javascript";
sTag2.src = "http://example.com/two.js";

var sTag1 = document.createElement("script");
sTag3.type = sTag3.type = "text/javascript";
sTag3.src = "http://example.com/three.js";

head.appendChild(sTag1); 
head.appendChild(sTag2); 
head.appendChild(sTag3); 

However, using YSlow, it shows that even though one.js, two.js and three.js are downloading in parallel - images are not loading until the last JavaScript is fully downloaded.

What can I do to allow images to not be blocked from loaded due to my JavaScript files downloading.

+1  A: 

Load your Javascript files right above the </body> tag.

Dustin Laine
Just tried it. Doesn't help.
Hacki
A: 

Where are you triggering that code from? Because you could wait to execute your quoted code until you see the window.load event, e.g.:

<script type='text/javascript'>
function loadMyScripts() {
    /* ...your loading code here...*/
}
window.onload = loadMyScripts; // Or use addEventListener/attachEvent to do it
</script>

The window.load event isn't fired until all of the images are loaded, so you'll be sure the scripts aren't getting in the way. Of course, it also leaves quite a large margin of time for the user to start doing things with the page, so you need to be sure the page doesn't need that JavaScript to be functional.

T.J. Crowder
I need this JavaScript available as soon as possible because it perform AJAX rendering visible to the user.
Hacki
@Hacki: Basically you have to choose which you want to give priority to, the AJAX rendering (visible to the user) or the image rendering (visible to the user). Other than that, you're left with the usual tricks: Combine the scripts so that they only take up one slot in the download queue, leaving (typically) another slot for the images to download in; minify the scripts; ensure they're being served with gzip compression; maybe move them (or the images) to a subdomain to increase parallelism (since browsers limit the number of concurrent requests to the same endpoint), [cont'd]
T.J. Crowder
@Hacki: [continuing] use a CDN or two, ensure that the scripts can be cached with expiration dates far in the future so this is only a first-time issue (you can use version numbers in the script names to force a new load when you change them), etc., etc. :-)
T.J. Crowder
@TJ Crowder, I'm using a CDN for my consolidated script (SimpleCDN). The other script is to load Google Maps from google.com. So I can't combine unfortunately.
Hacki