Which function is used to prevent code running before document loading in jQuery ? Last Updated : 30 Dec, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to prevent code from running, before the document is finished loading. Sometimes we might need to run a script after the entire document gets loaded, for example, if we want to calculate the size of images in the document then we need to wait until the entire document gets loaded. The ready() method is an inbuilt method in jQuery that executes the code when the whole page is loaded. This method specifies the function to execute when the DOM is fully loaded. Syntax: $(document).ready(function) Parameters: This method takes one function as an argument. It is used to specify the function to run after the document is loaded. CDN link: <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script> Approach: We can do this by using the jQuery ready() function.Create an HTML page and use the <script> tag which specify the src to embed the above jQuery CDN link.In ready() method, write the function which need to be prevented form execution before the entire document loads. Example: In the below code, line 20 is in ready() method, so lines 19 and 25 are executed and printed in the console before line 20, The line 20 is executed after the entire document gets loaded. HTML <!DOCTYPE html> <html lang="en"> <head> <script src= "https://code.jquery.com/jquery-1.9.1.min.js"> </script> </head> <body> <script> console.log("This is on line 19 "); $(document).ready(function () { console.log("this line will execute after" + " the webpage is loaded (on line 20)"); }); console.log("This is on line 25 "); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article Which function is used to prevent code running before document loading in jQuery ? P pulamolusaimohan Follow Improve Article Tags : HTML jQuery-Methods HTML-Questions jQuery-Questions Similar Reads How to run a code on document ready event in jQuery ? In this article, we will see how to run a code when a page loads using jQuery. To run a code at page load time, we will use the ready() method. This method helps to load the whole page and then execute the rest code. This method specifies the function to execute when the DOM is fully loaded. Syntax: 1 min read What is the starting point of code execution in jQuery ? The jQuery starts its code execution from the $(document).ready() function which is executed whenever the whole HTML DOM is loaded and is totally rendered by the browser, so that the event handlers work correctly without any errors. This $(document).ready() function load the script only after the wh 2 min read What is the use of ready() function in jQuery ? In this article, we will see how to use ready() function provided by the jQuery library. The ready() function is used to execute some javascript code only when the HTML DOM is fully loaded. We should not manipulate the DOM until it is fully loaded. ready() method comes very handy to detect when the 1 min read How to delay document.ready() method until a variable is set in jQuery ? In this article, we will learn to delay document.ready until a variable is set. The document.ready() method is used to execute some jQuery or JavaScript scripts when the HTML DOM is completely loaded. There are two approaches that can be followed here: Approach 1: Using the holdReady() method in th 3 min read What is the equivalent method of document.createElement() in jQuery ? jQuery is a popular JavaScript library that simplifies the process of manipulating HTML elements and creating dynamic web pages. In jQuery, there are several ways to create new HTML elements dynamically. One of the common ways is to use the $() method to create new elements, but there is also a meth 3 min read How to run a function when the page is loaded in JavaScript ? A function can be executed when the page loads successfully. This can be used for various purposes like checking for cookies or setting the correct version of the page depending on the user's browser. Below are the approaches to run a function when the page is loaded in JavaScript: Table of Content 2 min read How to prevent overriding using Immediately Invoked Function Expression in JavaScript ? Overriding is basically when you define multiple functions or variables that have the same name, the last one defined will override all the previously defined ones and every time when you invoke a function, the last defined one will get executed. Overriding usually happens when you have multiple jav 2 min read Difference between body.onload() and document.ready() function The body.onload() event will be called once the DOM and all associated resources like images got loaded. Basically, onload() will be called when the page has been fully loaded with entire images, iframes and stylesheets, etc. For example, if our page contains a larger size image, so onload() event w 2 min read How to check if a jQuery plugin is loaded? There are multiple ways by which we can simply check whether the jQuery plugins are loaded successfully or not using jQuery. We can also check whether a particular function within the plugin is accessible or not. This tutorial will demonstrate how to check if a jQuery plugin is loaded or not. Step 1 3 min read Why error "$ is not defined" occurred in jQuery ? One of the most common errors faced by jQuery developers is the '$ is not defined' error. At first, it may seem like a small error, but considering the fact that more than 70 percent of the website uses jQuery in some form or other, this may turn out to create a huge mess. Reason behind this error: 2 min read Like