Forum Moderators: open

Message Too Old, No Replies

precaching images

simple script that can drive a man crazy...

         

don costello

3:04 pm on Jan 24, 2008 (gmt 0)

10+ Year Member



hi guys,

the problem i am having is probably a relatively dummy one but i just cant seem to find my way out of it...

i wanna preload a couple of images, but without specifying their names, but with a generic function

thats the code: (a lot of variables i know, but that way i find it easier to keep track of stuff :)


function preload(ext) {
var imageCache = new Array();
var imgArray = document.getElementsByTagName("img");
for (i=0; i<imgArray.length; i++) {
if (imgArray[i].className == "preload"){
var filepath = imgArray[i].src;
var sl = filepath.length-4;
var filename = filepath.substring(sl, 4);
imageCache[imageCache.length] = new Image();
imageCache[imageCache.length-1].src = filename + "_over." + ext;}
}
}

as you can see i grab all img tags with a class="preload", crop away last for characters (.gif or .png), add _over and join this all back with the appropriate extension

then i am just invoking the function, for example preload('gif') within body onload

strangely, it does not seem to work

i tried 'alerting' every part of the code -> everything seems to be fine

damn me, but what am i doing wrong?

Fotiman

4:00 pm on Jan 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your substring is incorrect. Try this:

var filename = filepath.substring(0, sl);

don costello

6:42 pm on Jan 24, 2008 (gmt 0)

10+ Year Member



aaaaaa i'm so blooody stupid! how did i manage to overlook that?

thats what i call coding reverse learning curve :) once you cant find a mistake in 10minutes, you are not going to find it during the next 3hr or more..

of course it works now, thanks a lot man, i really appreciate that! cheers!