SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation I Am Having Some Problems With My Preload Images Script!!

    I am using the below preload images script and It seems to not be working...

    PHP Code:
    <script>
    function 
    doPreload()
    {

       var 
    the_images = new Array('/images/pd2.gif','/images/explore2.gif',
       
    '/images/games2.gif','/images/shops2.gif','/images/chat2.gif',
       
    '/images/mail2.gif','/images/help2.gif','/images/login2.gif',
       
    '/images/logout2.gif');
       
    preloadImages(the_images);
    }

    function 
    preloadImages(the_images_array) {

       for(var 
    loop 0loop the_images_array.lengthloop++)
        
       {
         var 
    an_image = new Image();
        
    an_image.src the_images_array[loop];
       }
    }
    </script> 

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not positive, but I don't think you can put linebreaks in an array declaration unless you use the literal notation. I also made a couple changes....
    Code:
    <script>
    
    function doPreload() {
    	var imgRoot = "/images/";
    	var the_images = [
    		'pd2.gif',
    		'explore2.gif',
    		'games2.gif',
    		'shops2.gif',
    		'chat2.gif',
    		'mail2.gif',
    		'help2.gif',
    		'login2.gif',
    		'logout2.gif'
    		];
    	preloadImages(the_images, imgRoot);
    	}
    
    function preloadImages(arrImages, root) {
    	for(var i = 0; i < arrImages.length; i++) {
    		var img = new Image();
    		img.src = root + arrImages[i];
    		}
    	}
    	
    </script>
    Using shorter variable names whenever possible is a good idea with JS. And, concatenating the root to each images at the time of src assignment saves variable space in the array, not to mention the time you spend re-typing or copy/pasting
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

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