Has the image loaded?

/ Published in: jQuery
Save to your folder(s)

Used this on a project to determine whether images were loaded or not. In an image carousel, for example, you could test the next/clicked image before advancing the slide, and if 'loaded' wasn't 'true', you could add a "loading" class and attach a new .load handler to remove the class and advance when the image finished loading.

Copy this code and paste it in your HTML
  1. $("img").each(function(){
  2. i = $(this).attr('src');
  3. $(this).attr('src','')
  4. $(this).load(function(){
  5. $(this).data('loaded','true');
  6. });
  7. $(this).attr('src',i);
  8. });

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.