Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is $(window).load() method in jQuery?
The code which gets included inside $(window).on("load", function() { ... }) runs only once the entire page is ready (not only DOM). This method waits for all content including images, stylesheets, and other resources to fully load before executing the callback function.
Note: The load() method was deprecated in jQuery version 1.8 and completely removed in version 3.0. To see its working, add jQuery version for CDN before 3.0.
Difference Between $(document).ready() and $(window).load()
The $(document).ready() method fires when the DOM is ready, while $(window).load() waits for all page resources to load completely. Use $(window).load() when you need to ensure images and other media are fully loaded.
Example
You can try to run the following code to learn how to use $(window).load() method in jQuery ?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("img").load(function(){
alert("Image successfully loaded.");
});
});
</script>
</head>
<body>
<img src="/videotutorials/images/tutor_connect_home.jpg" alt="Tutor" width="280" height="236">
<p><strong>Note:</strong> The load() method deprecated in jQuery version 1.8. It was completely removed in version 3.0. To see its working, add jQuery version for CDN before 3.0.</p>
</body>
</html>
Modern Alternative
For jQuery versions 3.0 and above, use the $(window).on("load", function() { ... }) syntax instead ?
$(window).on("load", function() {
console.log("All page resources have loaded completely.");
});
Conclusion
The $(window).load() method ensures all page content loads before executing code, making it ideal for working with images and external resources. Always use the modern .on("load") syntax for current jQuery versions.
