Another questionable jQuery Quiz answer at W3Schools

落爺英雄遲暮 提交于 2019-12-04 17:37:28

The question is ambiguous.

The correct answer depends on your definition of document. If it is the DOM, it would be B. If it were the entire page's assets, it would be $(window).load(function() { ... }).

As you can see, this quiz sucks.

From the jQuery API documentation...

.ready()

Description: Specify a function to execute when the DOM is fully loaded. While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed.

More about jQuery Ready

Defining DOM, from the W3C...

What is the Document Object Model?

The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents.

Defining .load() from the jQuery API documentation

.load()

Description: Bind an event handler to the "load" JavaScript event.

Example: Run a function when the page is fully loaded including graphics.

$(window).load(function () {

 // run code 

});


After researching this and much contemplation, I've come to the conclusion that the wording of the question is incorrect given their three choices. Of course, based on this line of thinking, since there is no correct answer to the quiz question, my original answer was also wrong.

The original W3Schools jQuery Quiz Question:

Which jQuery function is used to prevent code from running, before the document is finished loading?

Let's now analyze the original three answers:

A. $(document).load()

This was my initial answer but after posting this question on StackOverflow, realized that $(document).load() is not valid code as far as I can tell. $(window).load() is what I originally had in mind. Using $(window).load(), you will prevent code from executing before the entire window has loaded including all it's elements, images, etc.

A cannot be the correct answer since the wording is "document.load" although should be "window.load".

B. $(document).ready()

This is the official answer and it's valid jQuery code. It triggers when the DOM is ready but before anything else has finished loading. I would argue that you cannot say the "document" has finished loading otherwise the word "document" and "DOM" would have the same meaning and be interchangeable.

B cannot be the correct answer because without the images and other assets, the page (document) has not finished loading.

C. $(body).onload()

C cannot be the correct answer simply because "onload()" is not part of the jQuery library.

Conclusion(s):

  1. As the question is worded, "Which jQuery function is used to prevent code from running, before the document is finished loading?", there is no correct answer from the three presented. $(window).load() should be the correct answer as it refers to the "page" or "document" as a whole and not just the DOM.

  2. To accept the official answer, "$(document).ready()", the original question should be re-written as follows: "Which jQuery function is used to prevent code from running, before the DOM is finished loading?"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!