views:

23

answers:

1

Hi everyone,

I'm trying find an explanation to the following question, I looked around and haven't found any awnser so far: What is the the difference between the Simon Willison's code for the AddLoadEvent function and the load function from jQuery?

Here are the links:

1 ) AddLoadEvent code : http://simonwillison.net/2004/May/26/addLoadEvent/

2 ) .load function jQuery see api load from jQuery

-----------Case# 1 (jQuery .load function)--------------

remplaceMissingImage(){
    //run code...
}


$(window).load(function () {
  // run code
  remplaceMissingImage();
});

-----------or Case# 2 (AddEventLoad - JS)---------------

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


remplaceMissingImage(){
    //run code
}

addLoadEvent(remplaceMissingImage());

Q: Would these two pieces of code do the same thing?

$(window).load(function()) vs AddLoadEvent

Thanks in advance, sethier

A: 

Yes, that code does exact the same thing. jQuery internally uses something very similiar toaddLoadEvent to chain up functions, except it works for all kinds of events for all kinds of elements, not just onload of window. It is largely based on Dean Edwards' addEvent solution. I'd recommend viewing the source of jQuery and searching for "edwards" to find this section of code; its flexibility is very impressive.

Josh Stodola
thank you very much. it's was very interessing to discover that Dean Edward's code hide behind jquery load.
ethiers