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

Despite the fact that both do the same thing, I just want to know if there is any particular advantage using one over the other?

Event.observe(window, "load", function(){ //do something }); window.onload = function(){ //do something } 
+4
source share
2 answers

The difference is that window.onload is defined in the DOM Level 0 event model and erases all previously registered events. This is a β€œnative” call from the old API.

Event.observe from the prototype javascript template will determine the best available event certificate. Facade. In modern browsers addEventListener will be called - attachEvent in the case of Internet Explorer below version 9. On older browsers, onload will be called.

Obviously, the facade will choose the best available option, for example, Event.observe for the prototype or .load in the case of jQuery, for example.

Methods from the DOM Level 2 event model are preferable to methods of the DOM Level 0 model, since they act as observers and do not erase previous handlers.

+11
source

Read the quirksmode.org introduction to events . The difference between the traditional model and advanced models that need a shell because M $ / does not support the W3 standard, and a distinction needs to be made.

0
source

Source: https://habr.com/ru/post/1439380/


All Articles