0

I am wondering how javascript functions load or run.

Take for example i have got these block of javascripts functions;

<span id=indicator></span>

function BlockOne(){
    var textToWrite = document.createTextNode("I am   ");
    document.getElementById("indicator").appendChild(textToWrite);
} 
//==========
function BlockTwo(){
    var textToWrite = document.createTextNode("Going   ");
    document.getElementById("indicator").appendChild(textToWrite);
} 
//=========
function BlockThree(){
    var textToWrite = document.createTextNode("To School   ");
    document.getElementById("indicator").appendChild(textToWrite);
} 

function RunAll(){
    BlockOne();
    BlockTwo();
    BlockThree();
}
window.onload=RunAll();

Please which of these block of function run first or in what order are they going to run.

12
  • 2
    In the first example BlockThree will run but none of the others Commented Mar 13, 2015 at 23:24
  • 1
    Pretty sure they run sequentially, and you forgot to add () to the end of each function. Commented Mar 13, 2015 at 23:24
  • window.onload=BlockOne=BlockTwo=BlockThree; <-- does not do what you think it does. Commented Mar 13, 2015 at 23:34
  • 1
    You should consider using window.addEventListener("load", yourFunction, false) instead of window.onload = yourFunction unless you need to support old browsers. Commented Mar 13, 2015 at 23:34
  • @epascarello so what about if we have a declaration of variables say var d=a=c=e=" "; Commented Mar 13, 2015 at 23:48

1 Answer 1

6

This:

window.onload=BlockOne=BlockTwo=BlockThree;

will result in only "BlockThree" running when the "load" event fires. The way that assignment statement is interpreted is as if it were written:

window.onload = (BlockOne = (BlockTwo = BlockThree));

The right-most = operator causes the symbol "BlockTwo" to be set to the same value as "BlockThree", and then the middle = assigns that value (still "BlockThree") to "BlockOne". Function definition statements bind the function name to a local symbol, but they're not special symbols; they're pretty much the same as ordinary var symbols.

When that's all done, the "onload" property of window is set to just one function reference, and that's "BlockThree". After that point the original functions associated with "BlockOne" and "BlockTwo" will no longer be referenceable; they're essentially gone.

The second will run none of them, because your "RunAll" function is missing the function call operator ( () ) for all three of the functions. If it were

function RunAll(){
  BlockOne();BlockTwo();BlockThree();
}

then all three would run, in the order listed left-to-right.

Sign up to request clarification or add additional context in comments.

6 Comments

You should explain that in the first example, you are assigning window.onload = BlockThree; BlockOne = BlockThree; BlockTwo = BlockThree
@soktinpk working on it - I was just having a sneezing fit :)
@Pointy so they run from up to down or from bottom to up
@Simlofi the order in which the functions are declared does not matter at all. What matters is the order in which the functions are called.
@Pointy still you have not answered my question but I appreciate your answer what i am asking is for any reason which one trigger first
|

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.