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
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The difference is that
window.onloadis 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.observefrom the prototype javascript framework will determine the best event attacher available. A facade pattern. In modern browsers, theaddEventListenerwill be called –attachEventin case of Internet Explorer below version 9. In old browsers theonloadwill be called.It obvious that a facade will choose the best option available, like
Event.observefor prototype or.loadin 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.