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
}
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
}
Share
Improve this question
asked Oct 12, 2012 at 12:30
ShreedharShreedhar
5,6403 gold badges24 silver badges27 bronze badges
2 Answers
Reset to default 11The 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.
Read quirksmode's introduction into events. The difference is between the traditional model and the advanced models, which need a wrapper because M$ does/did not support the W3-standard and a distinction needs to be made.