Home > Uncategorized > Javascript redirect after images have loaded

Javascript redirect after images have loaded

This is a handy piece of javascript that can be used to execute a redirect only after all images have loaded on a page, up to a maximum of 10 seconds. I put in this maximum threshold, to account for the possibility of a bad link on the page, or a user with images turned off on their browser.

waitForImages();
var imageLoadTimeout = 10; // max 10 seconds;
var timeWaited = 0;
function waitForImages()

 var AllLoaded=true;
 if (timeWaited>imageLoadTimeout)
 {
  // redirect after 10 seconds, regardless of whether images are loaded.
  ImagesLoaded();
  return;
 } 
 for(i=0;i<document.images.length;i++)
 {
  var image = document.images[i];   
  if (!image.complete && image.name != "brokenImage" )
  {
  
   AllLoaded=false;
   break;
  }
 }  
 if (AllLoaded)
 {
  ImagesLoaded();
 }
 else
 {
  timeWaited += 0.5;
  setTimeout(‘waitForImages()’,500);
 }
}

function ImagesLoaded()
{
 RedirectPage();
}

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: