jQuery load

The load () method is used to load a specific element. It attaches an event handler to load event. It was deprecated in jQuery 1.8 version of jQuery library.

The load event occurs when a specific element is loaded. It is generally used with a URL (image, script, frame, iframe), and the window object.

Note: On some browsers, the load event did not trigger if the image is cached.

Syntax:

$(selector).load(function)  

It adds a function to the load event.

Parameters of jQuery load() event

ParameterDescription
FunctionIt is an essential parameter. It executes itself when the specified element is done loading.

Example of jQuery load event

Let’s take an example to demonstrate jQuery load() event.


  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     $("img").load(function(){  
  8.         alert("Image loaded.");  
  9.     });  
  10. });  
  11. </script>  
  12. </head>  
  13. <body>  
  14. <img src="good-morning.jpg" alt="good morning">  
  15. <p><b>Note:</b> On some browsers, the load event did not trigger if the image is cached.</p>  
  16. </body>  
  17. </html>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *