Hi experts

After seeing some really nasty cool stool over on
http://dean.edwards.name/weblog/2006/06/again/, I decided to
reimplement some code on a hobby site.

The original code makes sure that various external pieces of
Javascript is executed in the correct order, and also hooks
window.onload in various places.

Events are inherently not a good solution to the problem, because
events work by a "fire-and-forget" design.  Once an event has fired,
scripts that were by chance not loaded at the time will never see the
event and so will fail.

I have therefore rolled a simple signal() / wait() scheme of my own.
A piece of code can simply name what it wants to wait for, and signals
are never un-signalled, of course, so waiters are sure to be called
even if what they're waiting for has already transgressed.

Example:

<html>
        <head>
                <script type='text/javascript'>
                        // wait() and signal() has to be included in all
                        // (separately parsed) Javascript code blocks.
                        function signal(flag) { document['flag_' + flag] = 
true; }
                        function wait(flags, callback) {
                                var timer = setInterval(function() {
                                        for (var i = 0; i < flags.length; i++) 
if (document['flag_' +
flags[i]]) flags.splice(i, 1);
                                        if (flags.length == 0) { 
clearInterval(timer); callback(); }
                                }, 50);
                        }
                </script>
        </head>

        <body onload='signal("ImgLoaded")'>

                <script type='text/javascript'>
                        // Display hints on monitor..
                        // (This could be in an external javascript or 
whatever.)
                        function go1() { alert('HTML loaded.'); }
                        function go2() { alert('Images loaded.'); }
                        wait(['HtmlLoaded'], go1);
                        wait(['ImgLoaded'], go2);
                </script>

                <img src='http://www.nasa.gov/images/content/
84857main_EC04-0325-23_lg.jpg' />

                <script type='text/javascript'>
                        signal("HtmlLoaded");
                </script>
        </body>
</html>

As far as I can see, a beautiful solution to my problem.  There's an
obvious ickyness in that the code has to be included in every single
file, since it's not built into the browser.  No big deal for me.  The
only thing that *really* bugs me is that last signalling, which
happens right before BODY and HTML is closed.

So here's my question for the experts!

At the time HtmlLoaded is signaled, is the DOM really completely
constructed or not?

If not, does the ending tags for BODY and HTML actually cause the
browser to change anything in the current DOM, or can the DOM be
"considered" complete at the above time?

Reply via email to