...

View Full Version : Preload src assignment returns [object]



HairyTeeth
04-13-2003, 10:05 PM
The script below concatenates 2 image arrays and uses the third to preload images from both arrays. Take note of the bold text.


<script type="text/jav&#97;script" language="jav&#97;script">
<!--;

//dummy data
function cons(n,s){
this.n = n;
this.s=s;
}

imgArray_1 = new Array();
imgArray_1[0]=new cons('image_1.gif','some_param');
imgArray_1[1]=new cons('image_2.gif',some_param');


imgArray_2 = new Array('foo_1.gif','foo_2.gif','f00_3.gif');

function preloading(){
var tmpArray = new Array();
var imgSrc;
var pth="images/";

for(i=0;i<imgArray_1 .length;i++){
tmpArray[i] = imgArray_1 [i].n;
}

preloadArray=tmpArray.concat(imgArray_2);

for(j=0;j<preloadArray.length; j++){
imgSrc = pth + preloadArray[j];
preloadArray[j] = new Image();
preloadArray[j].src = imgSrc;

}

}

preloading()
//-->
</script>


However, If I state the image src like this (ater removing the imgSrc variable assignments)


preloadArray[j].src = pth + preloadArray[j]

...it returns (on my machine)
file:///c/...blah/images/[object]
instead of
file:///c/...blah/images/image_1.gif

Why is [object] returned instead of the file name? :confused:

Roelf
04-14-2003, 08:30 AM
thats because your first array (imgarray_1) contains objects. You create them yourself:
imgArray_1[0]=new cons('image_1.gif','some_param');
the other array, contains just imagenames

So do a check for the array-element being an object or a string. If its an object, you have to use the n-property of this object

HairyTeeth
04-14-2003, 08:50 AM
Yep..i thought it was wrapped up with the constructor...but how does the reassignment work:
//
var imgSrc;
...

imgSrc = pth + preloadArray[j]
...

preloadArray[j].src = imgSrc
//

How does simply switching the variable assignment around actually resolve to a string(?)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum