Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference of placing the event listener on the HTML tag versus the BODY tag?

I was looking at google.com source and saw:
<!doctype html><html onmousemove="google&&google.fade&&google.fade(event)">

I did not know the HTML tag could accept event listeners. What is the difference of placing the event listener on the HTML tag versus the BODY tag? Is there any difference in the event bubbling?

like image 236
Christopher Altman Avatar asked Jul 24 '10 23:07

Christopher Altman


People also ask

What is the difference between event handlers and event listeners?

Note: Event handlers are sometimes called event listeners — they are pretty much interchangeable for our purposes, although strictly speaking, they work together. The listener listens out for the event happening, and the handler is the code that is run in response to it happening.

What is the difference between Onclick and event listener?

addEventListener can add multiple events to a particular element. onclick can add only a single event to an element. It is basically a property, so gets overwritten.

What is event listener in HTML?

The addEventListener() method attaches an event handler to an element without overwriting existing event handlers. You can add many event handlers to one element. You can add many event handlers of the same type to one element, i.e two "click" events.


1 Answers

I saw this a couple of days ago and didn't give it much thought. But one reason could be "performance" as it always is with Google :)

For an extremely slow client, <html> will be parsed first thing and the onmousemove handler will be ready to fire if the user moves the mouse. If there is too much content inside <head>, this may be more preferable as the onclick on <body> may take a little while to register as all that head content has to be downloaded and parsed first.

Actually that is already happening, there is approximately 2KB of content before <body> appears.

like image 91
Anurag Avatar answered Oct 01 '22 19:10

Anurag