:::: MENU ::::
Posts tagged with: jQuery

Difference between jQuery ‘$(document).ready()’ and ‘$(window).load()’ function & usage

This post lead me to write, when i was using the jQuery ‘$(document).ready()’ function & struck with the issue. I was adding the css class

To know the difference, it’s important to understand the three things:
1. $(document).ready()
Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.

2.What is DOM?
DOM stands for Document Object Model, It defines the logical structure of documents and the way a document is accessed and manipulated. It is based on an object structure that closely resembles the structure of the documents it models. For instance, consider this table, taken from an HTML document:

In the above example, the root node is table. The table node has two child nodes; tr and td.

DOM is the logical structure which is very much like a tree; to be more precise, which is like a “forest” or “grove”, which can contain more than one tree.
3. $(window).load()

The window load event executes a bit later when the complete page is fully loaded, including all frames, objects and images. Therefore functions which concern images or other page contents should be placed in the load event for the window or the content tag itself.

Sometimes if you want to manipulate pictures. For example you want to verticaly and horizontaly align a picture and you need to get the width and height of the picture in order to do that. With $(document).ready() you won’t be able to do that if the visitor doesn’t have the image already loaded, in this case window.load can initialize the jquery alignment function when the image finishes loading.

Simple image mouseover through jQuery

Mouseover is common behavior for the images, required in any website across the pages. So it’s a good idea to keep this functionality in common script file. Simple jQuery code can make the life easier, just follow the below steps:

Step1: Add the jquery library, if not added or can be used google CDN hosted:

Step2: Add the below code within the script tag

Step3: Add the css class ‘rollover’, in which image you want mouseover

Step4: Add the images ‘normal’ & ‘mouseover’ in same location, ending with ‘imagename_off.gif’ & ‘imagename_on.gif’.