1

Some time ago I asked a question about the same issue, @BenAlabaster explained it thoroughly in his answer, I guess I am still missing a piece of logic here.

I tried to call functions that are assigned to variables like I've shown below, in the last function. Well it didn't work. Assuming it happened because the variable functionThree is outside the addOnload() function, I tried to move it inside and the browser just crashed.

addOnload(functionOne);
addOnload(functionTwo);
addOnload(functionThree);

function addOnload(newFunction){
    oldOnload = window.onload;

    if(typeof oldOnload == "function"){
        window.onload = function(){
            if(oldOnload){
                oldOnload();
            }
                newFunction();    //This is what I missed, and that's what caused the crash. 
        }
    }
    else{
        window.onload = newFunction;
    }
}   

function functionOne(){
    alert("This is the first function on this page!");
}

function functionTwo(){
    alert("This is the second function on this page!");
}

functionThree = function(){
    alert("This is the third function on this page!");
}
2
  • Like @Tomalak said below, I moved ' functionThree = function()' inside the 'addOnload' function to declare the variables. And the browser hanged because of a mistake I made. Commented Aug 29, 2010 at 21:25
  • 1
    Also see Function expressions vs. Function declarations Commented Aug 29, 2010 at 21:41

2 Answers 2

3

There is a slight issue with your addOnloadFunction: it should be:

function addOnload(newFunction){
    var oldOnload = window.onload;

    if(typeof oldOnload == "function"){
        window.onload = function(){
            newFunction();
            oldOnload();
        }
    }
    else{
        window.onload = newFunction;
    }
}   
Sign up to request clarification or add additional context in comments.

1 Comment

Your version is still missing the "var" keyword declaring oldOnload as a local variable
3

This happens because you use functionThree before it is defined.

function x() { 
  // a function that can be used before its declaring block
}

x = function() {
  // a variable that gets assigned a function object when the execution
  // of the program reaches this point, but not before
}

Apart from that, your implementation of addOnload() should go something like this:

function addOnload(newFunction){
  if (typeof window.onloadFunctions === "undefined") {
    window.onloadFunctions = [];
    window.onloadFunctions.push(window.onload);

    window.onload = function() {
      for(var i=0; i<window.onloadFunctions.length; i++) {
        var f = window.onloadFunctions[i];
        if (typeof f === "function") f();
      }
    }
  }
  window.onloadFunctions.push(newFunction);
}   

2 Comments

I just moved the declaration inside and it works. The browser hand because of a different error I made.
I will check yours too, but the one I posted works fine + it's short.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.