Subject: multiple onLoad triggers: howto

Posted on: 13/10/2013 06:47am
By: remy

Some javascripts, some blocks and some staticpages need to controle the window.onLoad event and do some stuff, like populating a drop down. They trigger the event window.onload or replace the function window.onload. A classic race condition.

This can be circumvented by defining a doubleDelegat function. This ensures that the various onLoad functions are executed one after each other.

PHP Formatted Code


// this should be in <head>…..</head>

<script language="JavaScript" type="text/javascript">
function makeDoubleDelegate(function1, function2) {
        return function() {
                if (function1)
                        function1();
                if (function2)
                        function2();
        }
}
</script>

// this is put in place where you need it

<script language="JavaScript" type="text/javascript">
myWindowInit=function() {
   // do your stuff
}
window.onload = makeDoubleDelegate(window.onload, myWindowInit);
</script>
 


Geeklog - Forum
https://www.geeklog.net/forum/viewtopic.php?showtopic=95279