Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  36] [ 4]  / answers: 1 / hits: 17694  / 12 Years ago, fri, october 12, 2012, 12:00:00

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
}

More From » prototypejs

 Answers
216

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.


[#82593] Wednesday, October 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeffery

Total Points: 180
Total Questions: 114
Total Answers: 117

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;