Detecting if images are loaded with JavaScript or jQuery

Posted on 2014-03-26
Last Modified: 2014-03-26
Hi E's, I have a project that the image are very important, and can not fail loading of images.
In this sense come here trying to get a solution to detect if the images were loaded.
I have this simple html code with 3 different images:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Imagem Load Detect</title>
#logo{position: relative; float: left;}
#home{position: relative; float: left;}
#contact{position: relative;}
<div id="logo"><img src="image_folder/logo.jpg" alt="logo"/></div>
<div id="home"><img src="image_folder/home.jpg" alt="home"/></div>
<div id="contact"><img src="image_folder/contact.jpg" alt="contact"/></div>

Open in new window

How I do for detect if the images are loaded or not in javascript or in Jquery?

The best regards, JC
Question by:Pedro Chagas
  • 3
LVL 82

Assisted Solution

leakim971 earned 500 total points
ID: 39957018
Test page :
jQuery(function($) {
    $("#logo img,#home img,#contact img").error(function() {
        var which_one = $(this).attr("alt");
        alert(which_one+" fail");

Open in new window

LVL 82

Accepted Solution

leakim971 earned 500 total points
ID: 39957024
if you add a class to each image it's better :

<img class="img_to_check" src="....

jQuery(function($) {
    $(".img_to_check").error(function() {
        var which_one = $(this).attr("alt");
        alert(which_one+" fail");

Open in new window


Author Comment

by:Pedro Chagas
ID: 39957088
Hi @leakim971, I deduce, for both solutions works, the attribute alt must always be present?

LVL 82

Expert Comment

ID: 39957105
No I display alt to let you know which one fail

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
parse url to form? 7 25
How to make a good PHP + MySQL + JS pagination system? 3 31
Filktering Alphabetically 8 28
Copying table data to one another 15 29
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question