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

17,287

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.

Share:
17,287
Shreedhar
Author by

Shreedhar

Front end Guy, love to architect, code and set up build tools for web apps. Here at Practo, I am doing what I love!!

Updated on June 07, 2022

Comments

  • Shreedhar
    Shreedhar about 2 years

    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
    }