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

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()

I faced a browser-related issue. Suppose we have some text field with size 100 along with some text area with same no of columns. In IE the size of text field and text area are same but in mozilla textarea will display with bigger length than text fields. This disturbs our layout and we want to resize this by detecting browser.

Mindfire Solutions

for mozilla we have to resize it as follows:

if((navigator.appName) == ‘Netscape’)

{ document.getElementById(‘mindfire’).cols = 41;

}

But problem is that the text area will resize after complete page load so when page loading is in process user will be able to view a wrong alignment and then the right one after complete page load.

With JQuery we can do this when a specific document element is loaded. For example, when the text area is loaded the related function will be fired, as given below.

$(‘mindfire’).ready(function()

{ if((navigator.appName) == ‘Netscape’){ $(“#mindfire”)[0].setAttribute(“cols”,41); }

});

150 150 Burnignorance | Where Minds Meet And Sparks Fly!