How to Call more than one function on loading the web page

If you’re using the following method to invoke more than one function then you’re stuck, you will be wasting you time on debugging trying to figure out what’s the problem.

  1. function callMe1() {
  2. ….
  3. }
  4.  
  5. function callMe2() {
  6. ….
  7. }
  8.  
  9. window.onload = callMe1;
  10. window.onload = callMe2;

callMe2 function will replace callMe1 function, and only callMe2 function will run.

The workaround of this would be using the Anonymous Function

  1. window.onload = function() {
  2.              callMe1();
  3.              callMe2();
  4. }

Another way to overcome this kind of situation is to use the addLoadEvent function

  1. function addLoadEvent(func) {
  2.      var oldonload = window.onload;
  3.      if (typeof window.onload != 'function') {
  4.                window.onload = func;
  5.      } else {
  6.                window.onload = function() {
  7.                      oldonload();
  8.                      func();
  9.                }
  10.      }
  11. }
  12.  
  13. use this as follows
  14.  
  15. addLoadEvent(callMe1);
  16. addLoadEvent(callMe2);

,

  1. No comments yet.
(will not be published)
Submit Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe to comments feed
  1. No trackbacks yet.

SetPageWidth