Results 1 to 2 of 2

Thread: What means "onload (function() {....} " ?

  1. #1
    Join Date
    Jan 2013

    Thumbs down What means "onload (function() {....} " ?

    I'm following the book "javascript definitive guide" and i found this example

    PHP Code:
    // Register the function f to run when the document finishes loading.
    // If the document has already loaded, run it asynchronously ASAP.
    function onLoad(f) {
    if (
    onLoad.loaded// If document is already loaded
    window.setTimeout(f0); // Queue f to be run as soon as possible
    else if (window.addEventListener// Standard event registration method
    else if (
    window.attachEvent// IE8 and earlier use this instead
    // Start by setting a flag that indicates that the document is not loaded yet.
    onLoad.loaded false;
    // And register a function to set the flag when the document does load.
    onLoad(function() { onLoad.loaded true; }); 

    I do not understand what means
    onLoad(function() { onLoad.loaded = true; });

    It seem like an abbreviated synthax of "addEventListener" method, but doing some proofs it never is executed...
    So, in this snippet i can't find the utility because no listener are initialized and no of his function will be executed

  2. #2
    Join Date
    Mar 2009
    Wherever i lay my hat
    I don't want to say something stupid, but since noone else replied, i'll try my explanation:

    First, you define a function onLoad which takes one argument - f. Then you set its public property loaded to false. This property signals, that the whole document has already been loaded. Then you call onLoad, passing another anonymous function as an argument.

    That means, inside the call of onLoad, f becomes equal to this anonymous function, making it no longer anonymous in onLoad's scope. Therefore, setTimeout, addEventListener and attachEvent can reference it as f.

    If you'd call onLoad another time with some different function as its argument, it would be added to setTimeout's queue with zero delay, which pretty much means invoked immediately.

    I'd be happy, if anyone corrects any mistakes, i may have written.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center