Current location: Hot Scripts Forums » General Web Coding » JavaScript » [SOLVED] Preloading images


[SOLVED] Preloading images

Reply
  #1 (permalink)  
Old 04-19-06, 05:11 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
[SOLVED] Preloading images

I wrote this script to preload images

JavaScript Code:
  1. <script type="text/javascript">
  2.     <!-- Preload images
  3.     window.onload = function()
  4.     {
  5.         var imageObj = new Image();
  6.         var images = new Array();
  7.         
  8.         images[0] = "images/1_p1290331.jpg";
  9.         images[1] = "images/1_p1290330.jpg";
  10.         images[2] = "images/1_p1290329.jpg";
  11.         images[3] = "images/1_p1290320.jpg";
  12.         images[4] = "images/1_p1290312.jpg";
  13.         images[5] = "images/1_p1290311.jpg";
  14.         images[6] = "images/1_1145093276_P1290320.JPG";
  15.         images[7] = "images/1_p1290314.jpg";
  16.                
  17.         for (i = 0; i < images.length; i++)
  18.         {
  19.             imageObj.src = images[i];
  20.         }
  21.     }
  22.     //-->
  23.     </script>

Somehow does it not seem like it's preloading anything. When I click on the thumbnail should the big image (which should be preloaded) appear in a div. This works, the only thing is that the images still take a while to load. Any ideas?

Last edited by TwoD; 07-23-07 at 10:27 AM. Reason: Replaced [code] with [highlight="JavaScript"]
Reply With Quote
  #2 (permalink)  
Old 04-19-06, 10:54 AM
jfulton's Avatar
jfulton jfulton is offline
Community VIP
 
Join Date: Apr 2006
Location: Los Angeles, CA
Posts: 660
Thanks: 0
Thanked 0 Times in 0 Posts
I'm not terribly familiar with preloading images...I'm assuming that if you just load the image, it will be cached and load quickly no matter how you try to access it? (ie. you don't have to access it via the javascript object because the image source is already in memory.)

If that's the case, then chances are you are not giving your images time to load in your for loop. Would this work?

JavaScript Code:
  1. for (i = 0; i < images.length; i++)
  2.         {
  3.             var imageObj = new Image();
  4.             imageObj.src = images[i];
  5.         }

Another question...does the window.onload event always occur before body.onload? All of the images should be loaded beforehand, right?

Last edited by TwoD; 07-23-07 at 10:26 AM. Reason: Replaced [code] with [highlight="JavaScript"]
Reply With Quote
  #3 (permalink)  
Old 04-19-06, 03:41 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
I think you need to keep an image-object for each of your images in memory for it to work best. That way the browser shouldn't first have to check the memory and find out it's not there, then the cache. But then again, I don't know if that's what it does at all...
Also make sure the browser doesn't try to use any of the preloaded images before they have been completely preloaded, otherwise they'll be downloaded again.

JavaScript Code:
  1. <!-- Preload images
  2.     window.onload = function()
  3.     {
  4.         var imgObj,images = ["images/1_p1290331.jpg",
  5.         "images/1_p1290330.jpg",
  6.         "images/1_p1290329.jpg",
  7.         "images/1_p1290320.jpg",
  8.         "images/1_p1290312.jpg",
  9.         "images/1_p1290311.jpg",
  10.         "images/1_1145093276_P1290320.JPG",
  11.         "images/1_p1290314.jpg"]
  12.                
  13.         for (i = 0; i < images.length; i++)
  14.         {
  15.             imgObj = new Image();
  16.             imgObj.src = images[i];
  17.             images[i] = imgObject;
  18.         }
  19.     }
  20.     //-->
  21.     </script>

EDIT: Forgot Image() only takes width and height as parameters.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.

Last edited by TwoD; 07-23-07 at 10:26 AM. Reason: Replaced [code] with [highlight="JavaScript"]
Reply With Quote
  #4 (permalink)  
Old 07-23-07, 07:23 AM
mahmoud82's Avatar
mahmoud82 mahmoud82 is offline
Wannabe Coder
 
Join Date: Nov 2006
Location: Jordan
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
hi
i try to use this code but it does not work with me. can u explain to me how can i use it.
and onther thing when i create a js code and i want from php to send an array to js, is this possible.
__________________
Their is no 100% secure system. ZCE
Reply With Quote
  #5 (permalink)  
Old 07-23-07, 10:25 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Do you get any errors? If so, please quote all of the error text and include the whole page, otherwise it's a bit hard to help fix it.
Do you have a testpage online where you tried using it? Then it'd be even easier since we could see what's going on in a "live" environment.

Also note that I wrote that last piece of code off the top of my head so I'm not sure if it actually does what it's supposed to. Correctly preloading images is a bit tricky in some circumstances...


If you want to create a JS Array from PHP, just echo it out to the page using normal JS syntax while it's being constructed by the server.
When finished, the Arrray should look something like:
JavaScript Code:
  1. var myArray = ["Entry 1","Entry 2","Entry 3","Entry 4"]
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #6 (permalink)  
Old 07-24-07, 01:36 AM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
I tried the same type of system you did, in the end i had to settle for the traditional way of...

image1 = new Image()
image1.src = 'images/img1.png';

image2 = new Image()
image2.src = 'images/img2.png';

/shrug
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Reply With Quote
  #7 (permalink)  
Old 07-24-07, 01:02 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
nova, do you have a testpage with the nonworking system?
Any errors etc?

It would be great if we could figure out why it doesn't work.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #8 (permalink)  
Old 07-24-07, 06:44 PM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
I honestly think this is the reason....

It tries to go to the next loop before the image had loaded - this then interrupts the pre-loading of the previous image and so on and so on, I never got an error in IE/Safari/or fire fox. It's the only thing I could think of.

I deleted my code (b/c i did not work) but it looked like that if I remember correctly.

EDIT:

I even tried creating new object i knew would not get written over by storing them in an array like so... (this did not work either)
javascript Code:
  1. var paths = ['asd0.jpg','asd1.jpg','asd2.jpg'];
  2. var images = Array();
  3. for(var i=0;i<paths.length;i++)
  4. {
  5. images[i] = new Image();
  6. images[i].src = paths[i];
  7. }

I settled on a PHP solution
PHP Code:

function preLoadImages($images)
{
$output '// Pre Load Images';
$num count($images);
for(
$i=0;$i<$num;$i++)
{
$output .= "\r\n".'image'.$i.' = Image();'."\r\n".'image'.$i.'.src = "'.$images[$i].'"'."\r\n";
}
return 
$output;
}
$images = array('asd0.jpg','asd1.jpg','asd2.jpg');
$js .= preLoadImages($images); 
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."

Last edited by nova912; 07-24-07 at 06:58 PM.
Reply With Quote
  #9 (permalink)  
Old 07-25-07, 03:23 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Quote:
Originally Posted by nova912 View Post
I honestly think this is the reason....

It tries to go to the next loop before the image had loaded - this then interrupts the pre-loading of the previous image and so on and so on, I never got an error in IE/Safari/or fire fox. It's the only thing I could think of.
If that's the case, wouldn't something like this work?
javascript Code:
  1. var images = ['image1.jpg', 'image2.jpg'];
  2. var loadcount = 0;
  3.  
  4. function preload()
  5. {
  6.     var image = new Image();
  7.     image.src = images[loadcount];
  8.    
  9.     image.onload = function()
  10.     {
  11.         if (loadcount < images.length)
  12.         {
  13.             loadcount++;
  14.             preload();
  15.         }
  16.     }
  17. }
  18.  
  19. window.onload = preload;

(Completely untested, just an idea...)
Reply With Quote
  #10 (permalink)  
Old 07-25-07, 11:26 AM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
Maybe, i never tried it. Give it a shot =X
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache/PHP not displaying images mdhall PHP 4 01-28-06 01:01 PM
Images system just like a news system modji Script Requests 0 01-02-06 08:12 PM
Royalty free cartoon images: Please reply mcrob The Lounge 2 10-04-05 09:40 AM
Cached images drivingme nuts Mikeirv ASP 0 08-16-04 11:58 AM
List images and zip before download jaimexyz Script Requests 0 09-01-03 03:01 PM


All times are GMT -5. The time now is 11:08 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.