Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.onload vs document.ready jQuery

I have a site with two columns. I want to have equal height on both using jQuery.

I'm trying to get the logo column height. I had:

$(document).ready(function() {     alert($('#logo').height()); });​ 

and it didn't work. So I changed it to:

window.onload = function(){     alert($('#logo').height()); } 

And it's working. What is going on in here?

like image 612
KryQ Avatar asked May 27 '12 23:05

KryQ


People also ask

What is the difference between window load and document ready function in jQuery?

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.

What is the difference between document ready and onload?

The main differences between the two are: Body. Onload() event will be called only after the DOM and associated resources like images got loaded, but jQuery's document. ready() event will be called once the DOM is loaded i.e., it wont wait for the resources like images to get loaded.

What is difference between document and window Load method?

The ready event occurs after the HTML document has been loaded, while the onload event occurs later, when all content (e.g. images) also has been loaded. The onload event is a standard event in the DOM, while the ready event is specific to jQuery.

What is the difference between window onload and document onload in JavaScript?

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.


1 Answers

document ready is fired when the DOM has loaded, so information like height isn't available, unless it's explicitly declared.

window onload waits for the assets in the page to be completely loaded - so information such as height is now available.

like image 126
ahren Avatar answered Sep 19 '22 22:09

ahren