CRT-600 practice question 20 of 30

A developer at Universal Containers is creating their new landing page based on HTML, CSS, and JavaScript. The website includes multiple external…

Full set: 223 questions
A developer at Universal Containers is creating their new landing page based on HTML, CSS, and JavaScript. The website includes multiple external resources that are loaded when the page is opened. To ensure that visitors have a good experience, a script named personalizeWebsiteContent needs to be executed when the webpage Is loaded and there Is no need to wait for the resources to be available. Which statement should be used to call personalizeWebsiteContent based on the above business requirement?
Answer options
A: windows,addEventListener('load', personalizeWebsiteContent);
B: windows,addEventListener('DOMContent Loaded ', personalizeWebsiteContent); Correct answer
C: windows,addEventListener('onload', personalizeWebsiteContent);
D: windows,addEventListener('onDOMCContentLoaded', personalizeWebsiteContent);
Correct Answer:
B. windows,addEventListener('DOMContent Loaded ', personalizeWebsiteContent);
Explanation

The DOMContentLoaded event is the most appropriate choice for this requirement. It fires as soon as the browser has finished parsing the HTML document and the Document Object Model (DOM) is ready. Crucially, it does not wait for external resources like stylesheets, images, or subframes to finish loading. This allows the personalizeWebsiteContent script to execute at the earliest possible moment without being delayed by network requests for other assets, ensuring a responsive experience for the visitor.

Why Incorrect

A. windows,addEventListener('load', personalizeWebsiteContent);
The load event only fires after the entire page, including all dependent resources (images, CSS), has completely loaded, which contradicts the requirement to not wait for these resources.
C. windows,addEventListener('onload', personalizeWebsiteContent);
The addEventListener method requires the event type name without the "on" prefix. The correct event type is 'load', not 'onload'.
D. windows,addEventListener('onDOMCContentLoaded', personalizeWebsiteContent);
Similar to option C, the correct event type for addEventListener is 'DOMContentLoaded', not 'onDOMContentLoaded'. The "on" prefix is incorrect syntax for this method. ---

References

1. Mozilla Developer Network (MDN) Web Docs

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." This directly supports the choice of option B as it meets the requirement to run without waiting for resources.

Source: MDN Web Docs

Document: DOMContentLoaded event. Available at: https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoadedevent

2. Mozilla Developer Network (MDN) Web Docs

load event: "The load event is fired when the whole page has loaded

including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded

which is fired as soon as the page DOM has been loaded

without waiting for resources to finish loading." This explains why option A is incorrect.

Source: MDN Web Docs

Window: load event. Available at: https://developer.mozilla.org/en-US/docs/Web/API/Window/loadevent

3. Mozilla Developer Network (MDN) Web Docs

EventTarget.addEventListener(): The documentation specifies the syntax as addEventListener(type

listener). The type parameter is a case-sensitive string representing the event type to listen for (e.g.

'click'

'load'

'DOMContentLoaded'). This confirms that using an "on" prefix like 'onload' is syntactically incorrect for this method

invalidating options C and D.

Source: MDN Web Docs

EventTarget.addEventListener(). Available at: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

Scroll to Top