I am working on the weather app. I found this example in StackOverFlow:
alert("Weather: "+ result.list.weather.description);
(see image and StackOverFlow URL)
My question: Why is the document.ready event necessary? Anyway the script element is in the end of the body element. Is it because the JSON request can take time to receive back, and we don’t want to delay the rest of the script from running?
Thanks in advance,
Personally I would always put it because when using jQuery I want to make sure the DOM is fully ready before I start manipulating it.
Hope it answered your question.
According to this: https://api.jquery.com/ready/
$(document).ready(handler) is depreciated as of jQuery 3.0, so they may be using an older version of jQuery. The docs say to use $(handler) instead now.
The linked doc also explains that .ready functions were handy because they would wait until the DOM was loaded, and if the DOM was already ready before .ready() was called by the code, it would still run.
The original poster probably thought it was necessary because $.getJSON() fires off an HTTP GET request and has to wait for a response from the server. According to the jQuery docs, $getJSON() accepts the URL, data, and then code to execute upon a successful response to the request as arguments.
I hope this helps. Happy coding,