Welcome to Geeklog Monday, August 20 2018 @ 12:23 pm EDT

Geeklog Forums

multiple onLoad triggers: howto


Status: offline

remy

Forum User
Full Member
Registered: 09/06/03
Posts: 141
Location:Rotterdam & Bonn
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>
 


All times are EDT. The time is now 12:23 pm.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content