View Full Version : Preload src assignment returns [object]

04-13-2003, 11: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;

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;


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




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)
instead of

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

04-14-2003, 09: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

04-14-2003, 09: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(?)