Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between AddLoadEvent and function load of jQuery

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)

function replaceMissingImage(){
    //run code here...
}


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

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();
    }
  }
}


function replaceMissingImage(){
    //run code here...
}

addLoadEvent(replaceMissingImage);

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

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

like image 770
ethiers Avatar asked Oct 27 '25 05:10

ethiers


1 Answers

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.

like image 86
Josh Stodola Avatar answered Oct 30 '25 14:10

Josh Stodola