Friday 27th July 2007

by Sajith M.R

image loaded

How can we check whether an image is loaded fully or
partially using javascript. It is possible
See the Example Below.

function IsImageOk(img) {
// During the onload event, IE correctly identifies
any images that
// weren’t downloaded as not complete. Others should too. Gecko-based
// browsers act like NS4 in that they report this incorrectly.

if (!img.complete) {
return false;
}

// However, they do have two very useful properties:
naturalWidth and
// naturalHeight. These give the true size of the image. If it failed
// to load, either of these should be zero.

if (typeof img.naturalWidth
!= “undefined” && img.naturalWidth
== 0) {
return false;
}

// No other way of checking: assume it’s ok.
return true;
}

//Call this function onLoad of body tag
function
checkImages() {
for (var i = 0; i < document.images.length; i++) {
if (!IsImageOk(document.images[i])) {

document.images[i].src = “/images/loaderror.jpg“;
}
}
}

· · · ◊ ◊ ◊ · · ·

4 Responses to “Javascript: Check an image is Loaded or Not”

  1. joy says:

    NOT UNDERSTAND

  2. Chris says:

    Hi Sajith,

    This is something new I am hearing. Will surely try this out next time, when in need.

    Thank You.

  3. David says:

    This is awsome. Ended my image loader head aches. Thank you!

  4. Alexandra says:

    Thank you!
    Very useful info.

· · · ◊ ◊ ◊ · · ·

Leave a Reply

وظائف 2011 تحويل العملات برامج برنامج تسريع التحميل برنامج الفلاش برنامج محول الصوتيات عربي hotel 2011 زيادة رواتب العساكر 1431