Forum Moderators: open

Message Too Old, No Replies

Get src height and width - for loop - Preloader to cache images

         

webaster

2:43 am on Mar 6, 2005 (gmt 0)

10+ Year Member



var photoNum = photoArray.length;
var photoDir = "path/to/images/";

var photoArray = new Array(
// filename, Width, Height, Caption
new Array("01.jpg", "400", "300"),
new Array("02.jpg", "400", "300"),
new Array("03.jpg", "400", "300"),
new Array("04.jpg", "400", "300"),
new Array("05.jpg", "400", "300),
new Array("06.jpg", "400", "300"),
new Array("07.jpg", "400", "300"),
new Array("08.jpg", "400", "300")

);

var myimages = new Array();

for (var i = 0; i < photoArray.length; i+=1) {

// here I need to extract the width and height from the PhotoArray

//How do I do this:
//Something like photoArray[i]. ...? extraxt width height from photoArray with for loop
//myimages[i] = new Image( photoArray[i]?, photoArray[i]?);
How to do the correct code?
myimages[i] = new Image(width,height);
// here I need to extract the filename from the PhotoArray
//same issue - extract filename with for loop
/Something like photoArray[i]. ...?
myimages[i].src = photoDir + filename;
myimages[i].id = "cacheimage" + i;
myimages[myimages[i].id] = myimages[i];
}

webaster

2:46 am on Mar 6, 2005 (gmt 0)

10+ Year Member



joined:Jan 4, 2005
posts:16
msg #:1
2:43 am on Mar 6, 2005 (utc 0)

var photoNum = photoArray.length;
var photoDir = "path/to/images/";

var photoArray = new Array(
// filename, Width, Height, Caption
new Array("01.jpg", "400", "300"),
new Array("02.jpg", "400", "300"),
new Array("03.jpg", "400", "300"),
new Array("04.jpg", "400", "300"),
new Array("05.jpg", "400", "300),
new Array("06.jpg", "400", "300"),
new Array("07.jpg", "400", "300"),
new Array("08.jpg", "400", "300")

);

var myimages = new Array();

for (var i = 0; i < photoArray.length; i+=1) {

// here I need to extract the width and height from the PhotoArray

//How do I do this:
//Something like photoArray[i]. ...? extraxt width height from photoArray with for loop
//myimages[i] = new Image( photoArray[i]?, photoArray[i]?);
//How to do the correct code?
myimages[i] = new Image(width,height);
// here I need to extract the filename from the PhotoArray
//same issue - extract filename with for loop
//Something like photoArray[i]. ...?
myimages[i].src = photoDir + filename;
myimages[i].id = "cacheimage" + i;
myimages[myimages[i].id] = myimages[i];
}

orion_rus

6:24 am on Mar 6, 2005 (gmt 0)

10+ Year Member



var photoNum = photoArray.length;
var photoDir = "path/to/images/";

var photoArray = new Array(
// filename, Width, Height, Caption
new Array("01.jpg", "400", "300"),
new Array("02.jpg", "400", "300"),
new Array("03.jpg", "400", "300"),
new Array("04.jpg", "400", "300"),
new Array("05.jpg", "400", "300),
new Array("06.jpg", "400", "300"),
new Array("07.jpg", "400", "300"),
new Array("08.jpg", "400", "300")

);

var myimages = new Array();

for (var i = 0; i < photoArray.length; i++) {
myimages[i] = new Image();
myimages[i].src=photoDir+photoArray[i][0];
myimages[i].width=photoArray[i][1];
myimages[i].height=photoArray[i][2];
myimages[i].id = "cacheimage" + i;
}

webaster

11:59 am on Mar 6, 2005 (gmt 0)

10+ Year Member



referring to myimages[i] = new Image(width,height);

myimages[i] = new Image(photoArray[i][1],photoArray[i][2]);

is the same as

myimages[i] = new Image();
myimages[i].width=photoArray[i][1];
myimages[i].height=photoArray[i][2];

webaster

12:16 pm on Mar 6, 2005 (gmt 0)

10+ Year Member



is there need for a onload handler

to achieve caching?

Bernard Marx

4:43 pm on Mar 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No. In fact, you're probably better off doing it immediately
- unless you have a specific reason to wait for the onload event.

BTW, specifying the images' W & H isn't actually necessary. Unless you need the information for some later purpose, just create image objects with no args:

blah = new Image()