Hi experts
A bit more on the topic of decoupling/timing load & execution of
javascript on various browsers.. It just occurred to me that the
example might be easier to read if we split it up:
Code to set and wait for signals:
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);
}
Above included in every file the browser loads.
Code to signal when images has been loaded:
<body onload='signal("ImgLoaded")'>
Code to signal when DOM has been constructed:
<script type='text/javascript'>signal("HtmlLoaded");</script>
</body>
</html>
Code in javascript that needs to execute first:
signal("MyScript1_Done");
Code in javascript that needs to execute after above, and with DOM and
images loaded:
wait(["HtmlLoaded", "ImgLoaded", "MyScript1_Done"], go);
...
signal("MyScript2_Done");
Hope the above is more readable..