Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
Why isn't document.onload called?

By looking at the output of console.log(document), I find document has these properties:onload=null;onloadstart=null;onloadend=null;onreadystatechange=null; I write the following code:

<html>
<head><title>test</title></head>
<body>
<script>
document.onload=function(){alert("document.onload");};
document.onloadstart=function(){alert("document.onloadstart");};
document.onloadend=function(){alert("document.onloadend");};
document.onreadystatechange=function(){alert("document.onreadystatechange");};
</script>
<div>hello</div>
</body>
</html>

Interestingly, document.onload,document.onloadstart,document.onloadend are never called, while document.onreadystatechange is called twice, why?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Why isn't document.onload called?

First of all the load events (created by the browser) do not bubble (because that's how it is specified):

document.body.onload = function(e) {
   console.dir(e.bubbles)
}

So you can only listen for these load events, on the element for which they occur.

And for Document there is no load event listed that will be triggered based on the standard.

readystatechange on the other hand is listed as an event that can happen for document.

You can however for sure listen for load events on the document and the event callback will be called. But that will only happen if you e.g. manually trigger one on document:

const event = new Event('load');

document.onload = function (e) { 
   console.log('load document')
}

document.dispatchEvent(event);

Or if you emit a load event that bubbles on a descendant:

const event = new Event('load', {bubbles: true});

document.onload = function (e) { 
   console.log('load document')
}

document.body.dispatchEvent(event);

So why does onload then appear as a property? That's due to the GlobalEventHandlers and Document includes GlobalEventHandlers), and due to event handler IDL attribute, exposing those as on… event properties.

about 4 years ago · Juan Pablo Isaza Report

0

.onload is part of the window object, not for document. GlobalEventHandlers.onload

The onload property of the GlobalEventHandlers mixin is an event handler that processes load events on a Window, XMLHttpRequest, <iframe> and <img> elements, etc.

And from what i've seen on MDN .onloadstart and .onloadend are used for resources like images, don't know if they are availiable for document/window. GlobalEventHandlers.onloadstart

But I think with onreadystatechange you should be able to emulate the behavior of onloadstart and onloadend.
Document.readyState

about 4 years ago · Juan Pablo Isaza Report

0

I'm unsure, but try this:

window.addEventListener('load', () => {
    alert("document.onload")
})

I only use window onload event, but if I never saw document onload event, just change window to document.

Like I said´, I'm unsure what to do, because the amount of your code, which you showed is to less.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!