Sorry if this has been answered before but all searches talk about the differences, not about using the two together, if possible.
Simply, can $(window).load.(function() {})
be used INSIDE of $(document).ready.(function() {})
?
I have some things that should be done after the DOM is loaded but then I want to reveal certain elements only after the images have finished loading. The only thing that works in Explorer 8, is putting the $(window).load
functions inside of the $(document).ready
with everything else.
Is this an acceptable practice?
I just want to use the most acceptable method to reveal a DIV
that contains small images, like a toolbar, after it's fully constructed. (visibility
hidden
versus display
none
, for example). The HTML for this DIV
is written by the code inside the $(document).ready
and then appended to the body using $('body').append()
before using the $(window).load
.
I'm having lots of problems with Explorer 8.
Simply, can $(window). load. (function() {}) be used INSIDE of $(document). ready.
The key difference between $(document). ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc.
The Difference between $(document). ready() and $(window). load() functions is that the code included inside $(window). load() will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the document ready event fires before all images,iframes etc.
The general idea is that window. onload fires when the document's window is ready for presentation and document. onload fires when the DOM tree (built from the markup code within the document) is completed.
This works fine and is an acceptable practice. After all, as you describe, there may be cases where the logic in the $(window).load()
handler depends on work taking place after the DOM is ready. If the window is in fact already loaded by the time you set up $(window).load()
, the handler will just fire immediately.
EDIT:
NOTE: This answer is only applicable to jQuery v2 and below.
jQuery .ready()
event:
The handler passed to
.ready()
is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.
jQuery .load()
event method:
The
load
event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.
Based on the jQuery documentation above, I've seen nothing that would indicate any issues with the following:
$(document).ready(function () {
// will fire IMMEDIATELY after the DOM is constructed
$(window).load(function() {
// will only fire AFTER all pages assets have loaded
})
});
Placing a .load
inside of a ready
simply guarantees that the DOM is ready whenever the load
is fired.
One may wish to place all jQuery code within a single DOM ready handler, and yet still may have a sub-set of jQuery code that requires all images be loaded first. This arrangement guarantees that all code be triggered once on DOM ready and the rest will be triggered subsequently whenever the page assets have finished loading.
This may ultimately be more of an issue of personal preference, however the OP was clearly asking if this arrangement would cause problems. This has not proven to be true.
I ran into this problem recently... from jQuery version 3, we can no longer put $(window).on('load') inside document.ready()... because ready handler are called asynchronously, which means ready can be called after load.
From jQuery Core Team: Github: jQuery 3 issues
To be clear, we understand what's causing this. We recently made ready handlers fire asynchronously. This has advantages that are hard to give up. The disadvantage is that the ready handler can sometimes fire after the load event if the load event fires quickly enough. The side effect you're seeing in this issue is that you're binding a load event handler after the load event has already fired.
The fix is to bind the load outside of ready.
$(function() { // Things that need to happen when the document is ready }); $(window).on("load", function() { // Things that need to happen after full load });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With