So let’s go over this piece by piece.
The $
is usually a reference to jQuery (https://jquery.com/) which is a library of a lot of really useful JavaScript functions and things.
$(document)
This is what’s known as a jQuery selector
. Essentially it locates whatever you put into it, and wraps it in a jQuery
object that lets you call various functions on it. In this case, it’s wrapping the entire document.
$(document).ready
The ready
function registers a jQuery
event handler that will be triggered when the element (in this case the document, I’m not sure if it can be added to other elements) has completed loading. In this case, the callback function you pass into it will run as soon as the entire webpage has loaded and is ready to begin processing. If you don’t do this, your JavaScript will execute immediately as it is parsed, which could cause problems if not all of your DOM is ready.
$(document).ready(function(){
...
});
This declares an anonymous function that is passed in as the first parameter to the ready
function. The contents of the function will be run as soon as the document is ready (as soon as the ready
event has been triggered).
console.log('Hello World!');
This will output the text Hello World!
to the console. In the case of a browser, it will show up in the browser console.
$(document).ready(function(){
console.log('Hello World!');
});
So this code will print the text Hello World!
to the browser console when the DOM, or web page, has finished loading and is ready to execute.
1
solved I am trying to figure out below javascript and what it does