Connecting Tech Pros Worldwide Forums | Help | Site Map

I have a preload script for an image gallery that only seems to work in Safari

Newbie
 
Join Date: Mar 2008
Posts: 1
#1: Mar 30 '08
The following is a script that I have written that preloads images for an image gallery. The problem is that the images only preload in Safari, other browsers load each image when it is called. Any ideas?

Expand|Select|Wrap|Line Numbers
  1. imgSeries = new Array();
  2. curImg = '';
  3.  
  4. function loadkill() {
  5.     document.getElementById('loadingtxt').style.display = "none";
  6.     document.getElementById('imgone').src = imgSeries[0];
  7.     document.getElementById('imgtwo').src = imgSeries[1];
  8. }
  9.  
  10.  
  11. for (i=0; i<=104; i++) {
  12.  
  13.     if (i<10) {
  14.         i = '00' + i;
  15.     } else if (i>=10 && i<100) {
  16.         i = '0' + i;
  17.     }
  18.     curImg = 'images/img' + i + '.jpg';
  19.     imgSeries.push(curImg);
  20. }
  21.  
  22. function preloadImgs() {
  23.     // Preload Images
  24.     preload_image_object = new Image();
  25.     for (i=0; i<=imgSeries.length; i++) {
  26.          preload_image_object.src = imgSeries[i];
  27.     }
  28. }
  29.  
  30. // Begin next, previous and indexing
  31. pg = 0;
  32. imgCt = imgSeries.length - 1;
  33.  
  34. function processPrevious() {
  35.     if (document.images && pg > 0) {
  36.         pg--;
  37.         document.getElementById('imgtwo').src = imgSeries[pg+1];
  38.         document.getElementById('imgone').src = imgSeries[pg];
  39.     }
  40. }
  41.  
  42. function processNext() {
  43.     if (document.images && pg < imgCt) {
  44.         pg++;
  45.         document.getElementById('imgtwo').src = imgSeries[pg+1];
  46.         document.getElementById('imgone').src = imgSeries[pg];
  47.     }
  48.  
  49. }

Expert
 
Join Date: Nov 2006
Posts: 392
#2: Mar 31 '08

re: I have a preload script for an image gallery that only seems to work in Safari


Did you enable the caching attribute(s) in the HTML meta tags? If not you should be able to google for examples of them. I do not have the syntax in front of me at the moment.
Familiar Sight
 
Join Date: Feb 2007
Posts: 207
#3: Apr 1 '08

re: I have a preload script for an image gallery that only seems to work in Safari


In this loop you are using i both as a counter and as an index
Quote:

Originally Posted by matt9807

Expand|Select|Wrap|Line Numbers
  1. for (i=0; i<=104; i++) {
  2.  
  3.     if (i<10) {
  4.         i = '00' + i;
  5.     } else if (i>=10 && i<100) {
  6.         i = '0' + i;
  7.     }
  8.     curImg = 'images/img' + i + '.jpg';
  9.     imgSeries.push(curImg);
  10. }

In the function below you create a single Image object, then in the time it takes to execute the loop, you assign multiple source files to it, each overwriting the previous.
Quote:
Expand|Select|Wrap|Line Numbers
  1.  function preloadImgs() {
  2.     // Preload Images
  3.     preload_image_object = new Image();
  4.     for (i=0; i<=imgSeries.length; i++) {
  5.          preload_image_object.src = imgSeries;
  6.     }
  7. }
  8.  
In the loop above, when i is equal to imgSeries.length, which element will be indexed?
Reply