How to run Javascript code before document is completely loaded (using jQuery)

Posted by eliza sahoo on Stack Overflow See other posts from Stack Overflow or by eliza sahoo
Published on 2010-06-14T09:28:21Z Indexed on 2010/06/14 9:32 UTC
Read the original article Hit count: 239

Filed under:

Hi all, I am sahring atip with you all.Please add on to this discussion.

JQuery helps faster page load than javascript. JQuery functions are fired when the related elements are loaded, instead of complete pageload. This is a common practice to call a javascript function when page is loaded like

window.onload = function(){ alert("Mindfire") } or

Inside of which is the code that we want to run right when the page is loaded. Problematically, however, the Javascript code isn't run until all images are finished downloading (this includes banner ads). The reason for using window.onload in the first place is due to the fact that the HTML 'document' isn't finished loading yet, when you first try to run your code. To circumvent both problems, jQuery has a simple statement that checks the document and waits until it's ready to be manipulated, known as the ready event

$(document).ready(function()

{ // Your code here });

© Stack Overflow or respective owner

Related posts about JavaScript