Shah Rahul
Shah Rahul

Reputation: 15

Load event not working while used with a selector element

Below is my <script> tag:

var2=document.querySelector('html')

var2.addEventListener('load',function(){alert('yo')});

why is onload not working?

And this is not just with html tag but with basically any selector.

Upvotes: 0

Views: 619

Answers (1)

Quentin
Quentin

Reputation: 943517

Most elements don't load content (the obvious exceptions being <iframe> and <img>) so load events don't fire on them.

If you want to capture the page's load event, then you shouldn't bind to any element:

addEventListener("load", yourEventHandlerFunction);

Upvotes: 1

Related Questions