JavaScript
  Home arrow JavaScript arrow Page 5 - Preloading Images with the DOM: The Introd...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Preloading Images with the DOM: The Introductory Process
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2005-11-02

    Table of Contents:
  • Preloading Images with the DOM: The Introductory Process
  • Setting up the basic scenario: building thumbnails and preloading large images
  • Coding the application: dynamically creating thumbnails with the DOM
  • Requesting data from the server: fetching images through an XML file
  • Preloading images in the background: defining the “preloadImages()” function

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Preloading Images with the DOM: The Introductory Process - Preloading images in the background: defining the “preloadImages()” function


    (Page 5 of 5 )

    As usual when working with asynchronous http requests, the core logic of most scripts is handled by the function attached to the “onreadystatechange” event handler. In this case, I’ve specified the “statusChecker()” function as the piece of code that checks for the progress in requesting the “images.xml” file, by triggering the “preloadImage()” function after this file has been fetched from the server. Thus, first I’ll list the “statusChecker()” function, and second show the code for “preloadImages()”. Here are the pertinent functions:

    function stateChecker(elemid){
        // if request is completed
        if(xmlobj.readyState==4){
            // if status == 200 display text file
            if(xmlobj.status==200){
                // preload images
                preloadImages();
     // display image
     displayImage(elemid);
     loaded=true;
            }
            else{
                alert('Failed to get response :'+ xmlobj.statusText);
            }
        }
    }
    // preload images
    function preloadImages(){
        // get image collection
        var imgcol=xmlobj.responseXML.getElementsByTagName('image');
        for(var i=0;i<imgcol.length;i++){
            // preload images
            pics[i]=new Image();
            pics[i].src=imgcol[i].firstChild.nodeValue+'.jpg';
        }
    }

    By dissecting the above listed functions, the program flow is quite easy to  follow. After the XML file has been successfully read, the corresponding images are actually preloaded within the “preloadImage()” function. Notice how the contents of all the <image> nodes are stored and buffered as image objects in the “pics” array:

    var imgcol=xmlobj.responseXML.getElementsByTagName('image');
    for(var i=0;i<imgcol.length;i++){
        // preload images
        pics[i]=new Image();
        pics[i].src=imgcol[i].firstChild.nodeValue+'.jpg';
    }

    This expression brings up to my mind an old favorite: image-based mouseovers. That’s one of the funniest things about AJAX, since it allows you to implement old techniques from a relatively new conception. By returning to the script, the line:

    pics[i].src=imgcol[i].firstChild.nodeValue+'.jpg';

    acts as the real preloader for each of the large images that will be displayed. Isn’t it good to reuse old concepts in modern applications?

    At this point, the preloading script is capable of creating the thumbnails, and then buffering all the large images from an XML file, whenever a user clicks on the image he or she wants to see. However, there are some functions remaining which need to be defined and explained. That’s where the last part of this series comes in, since I’ll be writing down the rest of the script, so you can use it in your web applications.

    Bottom line

    Having written some of the core functions that compose the image preloading script, it’s time to pause for a while and recapitulate the concepts and examples  deployed above. Hopefully, I’ve provided you with a hands-on example for preloading images with the DOM and AJAX. Even when the script may not meet all your needs, it can be used as an introductory process for building more complex applications.

    As I mentioned before, the last part of this series will be oriented to developing the remaining script functions, required for displaying large images. In the meantime, play around with the code and enjoy!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · The first part of this series goes through the development of an AJAX-based image...
       · Hi Alejandro, I haven't had time yet to try this idea, but I wanted to drop a...
       · Thank you for commenting on my article. I really appreciate it. With reference...
       · Though I can't recreate in my homepage, I still think this is an great idea. Would...
       · Thank you for commenting on my AJAX article. Unfortunately, I can't upload a demo...
     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek