Another Query Quiz's dubious answer to W3Schools

There's a jQuery poll posted on W3Schools ...

http://www.w3schools.com/quiztest/quiztest.asp?qtest=jQuery

Question number 16 is as follows:

What jQuery function is used to prevent code from running before the document finishes loading?

but. $ (Document) .load ()

B. $ (Document) .ready ()

C. $ (Body) .onload ()

I misunderstood this by choosing A.'s answer (their official answer is B).

I answered, thinking that I knew the following:

  • document.load starts after everything on the page loads including all images

  • document.ready is launched only after loading the DOM (not necessarily all images have been loaded)

I interpreted the original wording of the question, “document completed”, to include everything (with all images). In the end, I wondered why they called it "document.load". In addition, for their answer to be correct, you must conclude that "DOM" is equivalent to "document". This does not seem to be correct otherwise, why is it called a “document object model” (DOM), and not just a “document”?

Despite the fact that W3School claims that B is the correct answer, what is actually the correct answer?

Thanks for your thoughts.

Sidenote: Quote from my own comments in my other related question ...

"I really study most of my jQuery on the jQuery website and start searching here at StackOverflow when I get stuck. I mostly played with the quiz at W3School because I studied their" prerequisites "for JQuery certification. Don't consider yourself an expert on jQuery. jQuery, but I easily scored 95% (19/20) in my quiz. Seeing the wording in this answer, I decided that I confirm what I already suspected by posting here. These types of certification are now changing. "


EDIT:

I notified W3School of the existence of this thread.


EDIT 2:

When I answered the original quiz question, I was thinking about $ (window) .load (), so my answer to the quiz was clearly wrong. I believe that this fact does not leave any of the three options for multiple choice as the correct answer. See my detailed answer below.

+2
source share
2 answers

The question is ambiguous.

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

As you can see, this poll sucks.

+5
source

From the jQuery API documentation ...

.ready ()

Description: Indicate the function to execute when the DOM is fully loaded. Although JavaScript provides a load event to execute code when a page is displayed, this event does not fire until all assets, such as images, have been fully received. In most cases, a script can be executed as soon as the DOM hierarchy has been completely built .

Learn more about jQuery Ready

DOM definition, from W3C ...

What is a document object model?

The document object model is a platform and language interface that will allow programs and scripts for dynamic access and update the content, structure and style of documents.

Defining .load () from jQuery API documentation

.load ()

Description: Bind an event handler to a JavaScript load event.

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

$ (window) .load (function () {

  // run code 

});


After studying this and much contemplation, I came to the conclusion that the wording of the question is incorrect, given their three options. Of course, based on this line of thinking, since there is no right answer to the quiz question, my original answer was also wrong.

Original W3Schools jQuery Quiz question:

What jQuery function is used to prevent code from being run until the document has finished loading?

Now we analyze the initial three answers:

but. $ (Document) .load ()

This was my initial answer, but after posting this question to StackOverflow I realized that $ (document) .load () is not valid code, as far as I can tell. $ (window) .load () is what I originally meant. Using $ (window) .load (), you will prevent code from executing before the entire window loads, including all its elements, images, etc.

A may not be the right answer because the wording is "document.load", although it should be "window.load".

B. $ (Document) .ready ()

This is the official answer and valid jQuery code. It starts when the DOM is ready, but before anything else finishes loading. I would say that you cannot say that the “document” completed the download, otherwise the word “document” and “DOM” will have the same meaning and be used interchangeably.

B cannot be the right answer, because without images and other assets the page (document) did not finish loading.

C. $ (Body) .onload ()

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

Conclusions):

  • How the question is formulated: “What jQuery function is used to prevent code from running before the document finishes loading?”, There is no correct answer from the three presented. $ (window) .load () should be the right answer as it refers to the "page" or "document" as a whole, and not just the DOM.

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

+2
source

All Articles