JS-Dev-101 practice question 13 of 25

A developer needs the function personalizeWebsiteContent to run when the webpage is fully loaded (HTML and all external resources). Which implementation should be used?

Full set: 148 questions
Answer options
A: Add a handler to the personalizeWebsiteContent script to handle the DOMContentLoaded event
B: Add a listener to the window object to handle the load event Correct answer
C: Add a listener to the window object to handle the DOMContentLoaded event
D: Add a handler to the personalizeWebsiteContent script to handle the load event
Correct Answer:
B. Add a listener to the window object to handle the load event
Explanation

The requirement is for the function to run after the HTML and all external resources (like images, stylesheets, and scripts) are fully loaded. The load event on the window object is specifically designed for this purpose. It fires only when the entire page and all its dependent resources have finished loading. In contrast, the DOMContentLoaded event fires earlier, once the HTML document itself has been parsed, without waiting for external resources.

Why Incorrect

A. The DOMContentLoaded event does not wait for external resources like images, so it does not meet the requirement.
C. The DOMContentLoaded event is incorrect because it fires before all external resources are fully loaded.
D. The load event is correct, but it should be attached to the window object to signify the entire page has loaded, not a specific script element.

References

1. MDN Web Docs, "Window: load event". "The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets, scripts, iframes, and images."

2. MDN Web Docs, "Document: DOMContentLoaded event". "The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading."

3. W3C, HTML Living Standard, Section 7.6.1 "The load event". "This event fires at the Window object after the document and all subresources have finished loading."

Scroll to Top