How to use the ready() method In JQuery
The ready() method in jQuery is used to execute code whenever the DOM becomes safe to be manipulated. It accepts a handler that can be passed with the function required to be executed. It will now invoke the function after the page has completed loading.
Syntax:
$(document).ready( functionTobeLoaded() )
Example: The below example will illustrate the use of the ready() method to load jQuery after the page is loaded.
HTML
<!DOCTYPE html> < html > < head > < title > How to load jQuery code after page loading? </ title > < script src = "https://code.jquery.com/jquery-3.3.1.min.js" > </ script > </ head > < body > < h1 style = "color: green" > w3wiki </ h1 > < b > How to load jQuery code after page loading? </ b > < p > The current datetime is: < span class = "date" ></ span > </ p > < script type = "text/javascript" > $(document).ready( showDatetime() ); function showDatetime() { document.querySelector(".date"). textContent = new Date(); } </ script > </ body > </ html > |
Output:
How to load jQuery code after loading the page?
In this article, we are going to discuss the different ways of loading the jQuery code after loading the page. The methods to accomplish this task are listed below:
Table of Content
- Using the on() method with the load event
- Using the ready() method
- Using the load() method