473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Preloading an array of images (in js)

Jezternz
145 100+
I have a way below, but it doesnt seem to work in Firefox or Chrome, any ideas? Improvements? Changes completly?

Expand|Select|Wrap|Line Numbers
  1. var wds_assets_for_preload = [
  2. [0, "./images/buttons/drag_toolbar.gif"], 
  3. [0, "./images/buttons/dropdown_mouseover.png"]
  4. ]
  5. var CurrentImage = new Image();
  6. for(var i = 0; i < wds_assets_for_preload.length; i++)
  7. CurrentImage.src=wds_assets_for_preload[i][1];
  8.  
Thanks, Josh
Dec 29 '08 #1
9 3485
gits
5,390 Expert Mod 4TB
could you tell a bit more - what is not working correctly? typically it is an async process and you may use an onload-handler in FF for example to be sure that the image is loaded before further processing ... like in the following example:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <script type="text/javascript">
  3.  
  4. function foo() {
  5.     var i = new Image;
  6.  
  7.     i.onload = function() {
  8.         document.getElementsByTagName('body')[0].appendChild(i);
  9.     };
  10.  
  11.     i.src = 'http://www.google.de/intl/de_de/images/logo.gif';
  12. }
  13.  
  14. </script>
  15.  
  16. <body onload="foo();">
  17. </body>
  18.  
  19. </html>
  20.  
kind regards
Dec 29 '08 #2
Jezternz
145 100+
Im not sure, I just noticed, after this "preload" the images in IE7 that are dynamicly set come up instantly, and in ff, they slowly popup, I assumed this meant that the preload worked in IE? :S

Thanks, Josh
Dec 29 '08 #3
Dormilich
8,658 Expert Mod 8TB
to check whether an image is completely loaded I'd go for the image.complete property....

regards
Dec 30 '08 #4
gits
5,390 Expert Mod 4TB
@Jezternz
i guess that is a caching issue ... just setting the src of an image loads the file but unless you use an onload or the complete-property you don't really 'know' when the sources are loaded completely.

kind regards
Dec 30 '08 #5
xNephilimx
213 Expert 100+
The json array is malformed!

Wrong:
Expand|Select|Wrap|Line Numbers
  1. var wds_assets_for_preload = [
  2.     [0, "./images/buttons/drag_toolbar.gif"],
  3.     [0, "./images/buttons/dropdown_mouseover.png"]
  4. ];
  5.  
Should be:
Expand|Select|Wrap|Line Numbers
  1. var wds_assets_for_preload = [
  2.     "./images/buttons/drag_toolbar.gif",
  3.     "./images/buttons/dropdown_mouseover.png"
  4. ];
  5.  
There are no literal indexes for arrays in json!
Dec 30 '08 #6
gits
5,390 Expert Mod 4TB
?? the OP just used an array that contains elements that are arrays itself ... and the construct is syntactically correct ... and even when it would be an ajax-response it would be correct syntax to be evaled when it is a string ... what if the first element of a subelement is needed for something else? how should it be transfered in such a structure that basicly seems to be an Array-structure? you just flattened the array and so dropped some information ... i think this would be a valid JSON-string in case it would be used as such ... but of course i could be wrong ;)

kind regards
Dec 31 '08 #7
Jezternz
145 100+
yehhh, I have the 0's there as my array was a bunch of assets to preload. "0" just meant image, so they are supposed to be there :S
Jan 1 '09 #8
gits
5,390 Expert Mod 4TB
does it work with an onload or checking for the complete-property?

kind regards
Jan 1 '09 #9
Jezternz
145 100+
Havent had any luck :( Have decided to scrap the image preload for now, I will likely try again later, bring this thread back up.again.
Jan 2 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Fabian | last post by:
var preload1 = new Image(); preload1.src = "/pic/yay.gif"; var preload2 = new Image(); preload2.src = "/pic/nay.gif"; The above is meant to preload image files, yes? Problem is, it doesnt seem...
3
by: Dave | last post by:
Hi, I have an app that uses rollover images, but the images are still pulled from the web server during a mouseover instead of being pulled from memory. The code is as follows: the...
2
by: Julie | last post by:
Hi, I'm trying to change images on a website without reloading the whole page and use the following code to preload the images: var preloadFlag = false; function preloadImages() { if...
2
by: windandwaves | last post by:
Hi Gurus Preloading images has got to be JS 101. However, it does not seem to be working. Here is the function that I am using. I added the alerts to make sure it is working and all the right...
40
by: Geoff Cox | last post by:
Hello, I am still having problems - apologies if the answer is in previous postings! I now have, in the header, <sctipt> var myimages=new Array();
2
by: utahwrx | last post by:
I currently have a Javascript application that randomizes about 200 images. The problem is that the images preload, which causes the entire site to not come up until all the images are loaded. I'd...
3
by: littleark | last post by:
Hi everybody, I have a typical javascript images preloader, it works fine both on Firefox and on IE in my intranet (local server). It works fine on the Internet (remote server) in IE. In...
7
by: Inny | last post by:
Hello again, Im using the code below in a child page (popup), the images are called from the parent page. When the changer is running, the child page goes white between images. I realise this is...
7
by: Keith Hughitt | last post by:
Hi all, I am having trouble preloading images in a javascript application, and was wondering if anyone had any suggestions. Basically I have a bunch of images stored in a database as BLOBs. At...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.