Results 1 to 5 of 5

Thread: Problem with Image Preload Please Help!

  1. #1
    Join Date
    Oct 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem with Image Preload Please Help!

    Hi everybody,

    i have problems with image preloading. I have written the following code.

    HTML Code:
    <script type="text/javascript" language="JavaScript">
    <!--
            var myimages=new Array();
            function preloadimages()
            {
                    for (i=0;i<allpicture.arguments.length;i++)
                    {
                            myimages[i]=new Image()
                            myimages[i].src=allpicture.arguments[i]
                    }
            }
    
        var allpicture = new Array();
        allpicture("/icons/system.gif","/icons/walk.bmp","/icons/start.jpg","/icons/stop.bmp");
    //-->
    </script>
    Into the body tag i have witten this:

    HTML Code:
    <body onLoad="preloadimages()" bgcolor="#E0E0E0" >
    But this function doesn't work. My firefox browser returns the following errors:

    Error: allpicture is not a function
    Error: allpicture.arguments has no properties

    Have anybody an idea what the problem is? I'm not a good javascript coder, but i need this feature. Please help.

    Sorry for my bad english.

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Thumbs up

    I've modified your code a bit, you can find the difference between them easily

    Code:
    <script type="text/javascript" language="JavaScript">
     <!-- 
     var myimages=new Array();
    
     function preloadimages() 
     { 
    	 for (i=0;i<allpicture.length;i++) 
    	 { 
     		myimages[i]=new Image();
    		myimages[i].src=allpicture[i];
    		alert(myimages[i].src);
    		
    	} 
    } 
    
    var allpicture = new Array("/icons/system.gif","/icons/walk.bmp","/icons/start.jpg","/icons/stop.bmp"); 
    //-->
    </script>
    You can remove the alert() that i've used within the for loop kept it just for testing purpose.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    language="JavaScript"
    The language attribute is deprecated.
    <!--
    ...
    //-->
    Unnecessary. The only reason this would be necessary is if you had reserved characters in the script (such as < or >), in which case the script should be included externally.

    codeexploiter's code will work (EDIT: sorry, won't work: preloadimages() is never called), but is more verbose than it has to be. Try:
    Code:
    <script type="text/javascript">
      function preloadImages() {
        for(var i = 0, s = preloadImages.store; i < arguments.length; ++i)
          (s[s.length] = new Image()).src = arguments[i];
      }
      preloadImages.store = [];
    
      preloadImages("/icons/system.gif","/icons/walk.bmp","/icons/start.jpg","/icons/stop.bmp");
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Twey i think you missed the second part of Krusty's post. He called the function in the onLoad event of body. I just rectified erroneous function thats all rest he has to do.

    Originally posted by: Krusty
    Into the body tag i have witten this:

    HTML Code:
    <body onLoad="preloadimages()" bgcolor="#E0E0E0" >

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah, OK.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •