views:

431

answers:

6

On my web site, I'm trying to accomplishes the fastest page load as possible.

I've noticed that it appears my JavaScript are not loading asynchronously. Picture linked below.

alt text

How my web site works is that it needs to load two external JavaScript files:

  • Google Maps v3 JavaScript, and
  • JQuery JavaScript

Then, I have inline JavaScript within the HTML that cannot be executed until those two files above are downloaded.

Once it loads these external javascript files, it then, and only then, can dynamically render the page. The reason why my page can't load until both Google Maps and JQuery are loaded is that - my page, based on the geolocation (using Gmaps) of the user will then display the page based on where they are located (e.g. New York, San Francisco, etc). Meaning, two people in different cities viewing my site will see different frontpages.

Question: How can I get my JavaScript files to download asynchronously so that my overall page load time is quickest?

UPDATE:

If I were to download, somehow, Google-maps and JQuery asynchronously, how would I create an event that would be fired once both Google-maps and JQuery have downloaded since my page has a hard dependency on those files to execute.

UPDATE 2

Even though there are 3 answers below, none still actually answer the problem I have. Any help would be greatly appreciated.

+5  A: 

HTTP downloads are generally limited by browsers to two simultaneous downloads per domain. This is why some sites have the dynamic content on www.domain.tla and the images and javascript on static.domain.tla.

But browsers act slightly differently with scripts, while a script is downloading, however, the browser won't start any other downloads, even on different hostnames.

The standard solution is to move scripts to the bottom of the page, but there is a workaround that might or might not work for you: Insert the script DOM element using Javascript.

voyager
That was also my first thought, but the both scripts *actually* originate from different domains. I'd after all just say: it's browser specific behaviour. In Firefox the downloads per domain is by the way configureable as `network.http.max-connections` in `about:config`. It defaults to 15.
BalusC
@BalusC: downloading a javascript file blocks all other downloads, regardless of domains. Also, defaults are too entrenched to rely on users changing them ;)
voyager
And the creator of YSlow, which you linked too, states that you **can** accomplish asynch JS download's - http://www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/ However, given my requirement that both Gmaps and JQuery need to be downloaded first, I'm not sure how to use Steve's example. *As such, I don't believe you answer is correct.*
Teddyk
@Teddyk: I'd use DOM injection, as all other workarounds seem even hackier, and more prone to get you more problems with cross browser support.
voyager
@voyager, but how do you create an event that can be fired once BOTH (google-maps and jquery) are downloaded before executing the inline JS code if using DOM injection? Make sense what I'm saying? Again, as a reminder - my page can't render until both Gmaps and Jquery have downloaded. What I'm trying to do is simply speed up the download process since my page had a hard dependency on those two files.
Teddyk
@Teedyk: try doing the DOM injection early on the page load (at the top). That tricks the browser into a non-blocking download. What happens if a user goes to your page with Javascript disabled?
voyager
@voyager, javascript is required. I still don't think you understand, even if I did a DOM injection early in the page ... I need to somehow halt on-page HTML JS execution until both google-maps and jquery have downloaded. How would I do that halt/event creation?
Teddyk
@Teddyk: there is no simple answer to what you are asking then. You either restructure your page in order for it not to be dependent on loaded javascript files, or do blocking javascript downloads. I can't think of any middle ground right now.
voyager
@voyager, what I'm asking to do is: 1) perform asynchronous JS download while also maintaining dependency on those files. Right now, just using <script> I get the dependency ability (b/c it blocks) but my JS stagger on download. If you look at the linked picture above, I should be able to move forward the JS that I drew an arrow too.
Teddyk
A: 

Regardless what order they download in, the scripts should be parsed/executed in the order in which they occur on the page (unless you use DEFER).

So, you can put both Google Maps first in the head, THEN JQuery. Then, in the body of your page somewhere:

<script language="Javascript">

    function InitPage() {
       // Do stuff that relies on JQuery and Google, since this script should
       // not execute until both have already loaded.
       }

    $(InitPage);   // this won't execute until JQuery is ready

</script>

But this does have the disadvantage of blocking your other connections while loading the beginning of the page, which isn't so awesome for page performance.

Instead, you can keep JQuery in the HEAD, but load the Google scripts from the InitPage() function, using JQuery's Javascript-loading functionality rather than the Google JSAPI. Then start your rendering when that call-back function executes. Same as the above, but with this InitPage() function instead:

  function InitPage() {
     $.getScript('Google Maps Javascript URL', function() {
        // Safe to start rendering now
        });
richardtallent
Would this still be asych? Since it appears to no start downloading the Google Maps until JQuery downloads first.
Teddyk
True, but JQuery is a fast load. The solution above solves the dependency problem, not async network connections.You can use the document.write hack at the top to start async loading of JQuery and Google, and try using DEFER on your main page code, but that won't guarantee that your main script will wait on the other scripts. To do that, you'll need your own "ready" function that tests for the JQuery and Google Maps objects, waits a few milliseconds and tries again if they aren't both available, and times out with an appropriate error if they don't load in a reasonable time period.
richardtallent
+2  A: 

You could use something like this, which works pretty well in most browsers. It has some issues in IE6 at least, but I don't really have the time to investigate them.

var require = function (scripts, loadCallback) {
    var length        = scripts.length;
    var first         = document.getElementsByTagName("script")[0];
    var parentNode    = first.parentNode;
    var loadedScripts = 0;
    var script;

    for (var i=0; i<length; i++) {
        script = document.createElement("script");
        script.async = true;
        script.type = "text/javascript";
        script.src = scripts[i];

        script.onload = function () {
            loadedScripts++;

            if (loadedScripts === length) {
                loadCallback();
            }
        };

        script.onreadystatechange = function () {
            if (script.readyState === "complete") {
                loadedScripts++;

                if (loadedScripts === length) {
                    loadCallback();
                }
            }
        };

        parentNode.insertBefore(script, first);
    }
};


require([
    "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js",
    "http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js",
    "http://ajax.googleapis.com/ajax/libs/yui/2.7.0/build/yuiloader/yuiloader-min.js"
], function () {
    console.log(jQuery);
    console.log($);
    console.log(YAHOO);
});
Ionuț G. Stan
But what would my callback function be if I need to wait until 2 JS files have been downloaded?
Teddyk
The callback is triggered after all the URLs in the array have been loaded. If you put two URLs in that array, the callback will be triggered after both of them have loaded. There may be more complex cases, but I do not pretend that I have offered an exhaustive solution. It is merely a starting point for your needs.
Ionuț G. Stan
+3  A: 

Hi. Someone asked me to comment on this thread, but that was before @lonut posted a response. @lonut's code is a very good solution, but I have some comments (critical and not so critical):

First, @lonut's code assumes that the scripts do NOT have load dependencies on the other scripts. This is a little hard to explain, so let's work with the simple example of jquery.min.js and prototype.js. Suppose we have a simple page that just loads these two scripts like this:

<script src="jquery.min.js"></script>
<script src="prototype.js"></script>

Remember - there's nothing else in the page - no other JavaScript code. If you load that page the two scripts get downloaded and everything's fine. Now, what happens if you remove the jquery.min.js script? If you get errors from prototype.js because it's trying to reference symbols defined in jquery.min.js, then prototype.js has a load dependency on jquery.min.js - you cannot load prototype.js unless jquery.min.js has already been loaded. If, however, you don't get any errors, then the two scripts can be loaded in any order you wish. Assuming you have no load dependencies between your external scripts, @lonut's code is great. If you do have load dependencies - it gets very hard and you should read Chapter 4 in Even Faster Web Sites.

Second, one problem with @lonut's code is some versions of Opera will call loadCallback twice (once from the onload handler and a second time from the onreadystatechange handler). Just add a flag to make sure loadCallback is only called once.

Third, most browsers today open more than 2 connections per hostname. See Roundup on Parallel Connections.

Steve Souders
@Steve Sounders, I'm **extremely** honored to have you comment on my post. With that being said, is there a solution to my question? In case it help, I have posted a 2nd question that is directly related to this problem at http://stackoverflow.com/questions/2804494/javascript-why-does-my-asrnchronous-download-not-download-asynchronously that might provide you some more detail. Again, many thanks and I overwhelmed that you're on StackOverlow :)
Teddyk
Indeed, I have intentionally ignored the first issue, as in my opinion it can be solved by issuing another call to `require` inside the callback. Haven't done any tests, but seems like it should work. Regarding the second issue, I knew there were some browsers in which the callback would trigger twice, but couldn't trigger the bug as I have the latest Opera version (and IE6 worked fine). Anyway, thanks for the feedback!
Ionuț G. Stan
+1  A: 

The LABjs dynamic script loader is designed specifically for this type of case. For instance, you might do:

$LAB
  .script("googlemaps.js")
  .script("jquery.js")
  .wait(function(){
     // yay, both googlemaps and jquery have been loaded, so do something!
  });

If the situation was a little more complex, and you had some scripts that had dependencies on each other, as Steve Souders has mentioned, then you might do:

$LAB
  .script("jquery.js")
  .wait() // make sure jquery is executed first
  .script("plugin.jquery.js")
  .script("googlemaps.js")
  .wait(function(){
     // all scripts are ready to go!
  });

In either case, LABjs will download all of the scripts ("jquery.js", "googlemaps.js", and "plugin.jquery.js") in parallel, as least up to the point the browser will allow. But by judicious use of the .wait() in the chain, LABjs will make sure they execute in the proper order. That is, if there's no .wait() in between the two scripts in the chain, they will each execute ASAP (meaning indeterminate order between tehm). If there's a .wait() in between two scripts in the chain, then the first script will execute before the second script, even though they loaded in parallel.

Kyle Simpson
+1  A: 

Move your javascript includes (<script src="...) from the HEAD element to the end of your BODY element. Generally whatever is placed in the HEAD is loaded synchronously, whatever is placed in the BODY is loaded asynchronously. This is more or less true for script includes, however most browsers these days block everything below the script until it is loaded - hence why having scripts included at the bottom of the body is best practice.

Here is the YUI guildline for this which explains it in further detail: http://developer.yahoo.net/blog/archives/2007/07/high_performanc_5.html

This is also the reason why stylesheets should be in the head, and javascript should be in the body. As we do not want to see our page turn from spaghetti to niceness while the styles load asynchronously, and we don't want to wait on our javascript while our page loads.

balupton