Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event.observe(window, "load", function(){..} v/s window.onload = function(){..}

Even though both do the samething, i just want to know is there any specific advantage using one over another?

Event.observe(window, "load", function(){
//do something
});

window.onload = function(){
//do something
}
like image 487
Shreedhar Avatar asked Oct 12 '12 12:10

Shreedhar


1 Answers

The difference is that window.onload is defined in the DOM Level 0 Event Model and will erase all earlier registed events. It's an 'native' call from an old API.

The Event.observe from the prototype javascript framework will determine the best event attacher available. A facade pattern. In modern browsers, the addEventListener will be called - attachEvent in case of Internet Explorer below version 9. In old browsers the onload will be called.

It obvious that a facade will choose the best option available, like Event.observe for prototype or .load in case of jQuery for example.

The methods from the DOM Level 2 Event Model are preferred over the DOM Level 0 Event Model methods because they act as observers and don't erase previous handlers.

like image 111
Andries Avatar answered Oct 06 '22 18:10

Andries