Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery events .load(), .ready(), .unload()

Just a simple question, for the jQuery event. Are the .load(), .ready() and .unload() run in order when the DOM is loaded? The answer seems yes when I see the jQuery Documentation.

<script type="text/javascript">      $(window).load(function () {         // run code         initializeCode();     });      $(document).ready(function() {         //run code that MUST be after initialize     });      $(window).unload(function() {         Cleanup();     }); </script> 

However, the code inside the .ready() is execute before the initializeCode(); is execute, so I feel really strange. And now I have to place my code inside the .onload() method and just after the initializeCode(); line, which means to be inside the .ready() block.

Could someone explain me more about this, as I am new to jQuery?

like image 425
Eric Avatar asked Apr 21 '10 13:04

Eric


People also ask

What is unload in jQuery?

The unload event occurs when the user navigates away from the page. The unload event is triggered when: a link to leave the page is clicked. a new URL is typed in the address bar. the forward or back buttons are used.

When load event occurs?

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded , which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading.

What is unload in JavaScript?

The unload event fires when a document has completely unloaded. Typically, the unload event fires when you navigate from one page to another. You can use the unload event to clean up the references to avoid memory leaks. Note that the web browsers with the popup blocker enabled will ignore all window.

How can we call a method after page load in jQuery?

after page loading? Method 2: Using the ready() method: 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.


1 Answers

NOTE: .load() & .unload() have been deprecated


$(window).load(); 

Will execute after the page along with all its contents are done loading. This means that all images, CSS (and content defined by CSS like custom fonts and images), scripts, etc. are all loaded. This happens event fires when your browser's "Stop" -icon becomes gray, so to speak. This is very useful to detect when the document along with all its contents are loaded.

$(document).ready(); 

This on the other hand will fire as soon as the web browser is capable of running your JavaScript, which happens after the parser is done with the DOM. This is useful if you want to execute JavaScript as soon as possible.

$(window).unload(); 

This event will be fired when you are navigating off the page. That could be Refresh/F5, pressing the previous page button, navigating to another website or closing the entire tab/window.

To sum up, ready() will be fired before load(), and unload() will be the last to be fired.

like image 183
Tower Avatar answered Oct 11 '22 00:10

Tower