Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2003
    Posts
    176
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Preload src assignment returns [object]

    The script below concatenates 2 image arrays and uses the third to preload images from both arrays. Take note of the bold text.
    Code:
    <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)
    Code:
    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?
    Last edited by HairyTeeth; 04-13-2003 at 11:11 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    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
    I am the luckiest man in the world

  • #3
    Regular Coder
    Join Date
    Mar 2003
    Posts
    176
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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(?)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •