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.

1 thought on “Load event not working while used with a selector element”

  1. 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);
    
    Reply

Leave a Comment