AddLoadEvent (func) function understanding -- "Javascript DOM programming art re

---------------------------------------------- personal learning arrangement, reproduced please specify the ----------------------------------------------

Previous attempts to the book image gallery example, found that the JavaScript effect how to have no effect. Find the source code to learn, it was found to be forgotten inside the addLoadEvent (func) function.

After seeing the also is not very understanding, Google some explanation, finally have a clear understanding of. The way to share my learning outcomes:

Before JavaScript does not play the role of reasons: because the JavaScript file execution order is top-down, and is included in the HTML file.<head>Tags inside the execution, so the execution time, and not the so-called element nodes, so the function does not play a role.

AddLoadEvent (func) function is JS function is all ready, after the whole HTML document download start again, this time, with the use of the element nodes, also play a role.

function addLoadEvent(func) {
  var oldonload = window.onload;              //The response function to the oldonload object, if oldonload is firstfunction (firstfunction) on the implementation of()
  if (typeof window.onload != 'function') {   //Judgment before calling the function
    window.onload = func;                     //Do not call the function on the execution of the current function
  } else {
    window.onload = function() {              //The call to the function call an anonymous function, before the function call and contains the current function
      oldonload();
      func();
    }
  }
}

I recommend a Book: "Javascript DOM programming art

Posted by Opie at December 01, 2013 - 5:52 AM