JavaScript
  Home arrow JavaScript arrow Page 3 - 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 - Coding the application: dynamically creating thumbnails with the DOM


    (Page 3 of 5 )

     

    Due to the fact that thumbnails will be displayed by using the DOM to alter the document tree, the first function to be defined is “createThumbnails()”, which accepts as a parameter the number of pictures to be created. Its definition is as follows:

    function createThumbnails(numpics){
        for(var i=0;i<numpics;i++){
            var cdiv=document.createElement('div');
            cdiv.className='thumbnail';
            var a=document.createElement('a');
            a.setAttribute('href','#');
            a.setAttribute('id',i);
            // create thumbnails
            var img=document.createElement('img');
            img.setAttribute('width','120');
            img.setAttribute('height','77');
            img.setAttribute('border','0');
            img.setAttribute('src','thumbnail'+(i+1)+'.jpg');
            img.setAttribute('alt','click to enlarge');
            a.appendChild(img);
            cdiv.appendChild(a);
            document.getElementsByTagName('body')[0].appendChild(cdiv);
            // assign 'onclick' event handler to <a> elements
            a.onclick=function(){
     // preload all images when the first image is clicked
     // or display the proper image
     (!loaded)?sendRequest(this.id,'images.xml'):displayImage(this.id);        
            }
        }
    }

    As you may have guessed, the function above is tasked with building the thumbnails once the whole page has been loaded. Stripped down to its bare bones, this function will fetch a given number of small images (for this example it will be only four pictures), which will be contained into a DIV element. Finally, they’ll be appended to the web document, by using the <body> element as the starting node to hook up the structure just created. However, if you’ve already predefined another element as a new insertion point, you may use it with minor difficulties.

    At this point, I should take for granted that adding thumbnail images with the DOM is pretty familiar to you. There’s still a crucial point to be studied within the above script though. Notice the piece of code below:

    a.onclick=function(){
        // preload all images when the first image is clicked
        // or display the proper image
        (!loaded)?sendRequest(this.id,'images.xml'):displayImage(this.id);        
    }

    As you can see, the function assigns an “onclick” event handler to each <a> element that houses a thumbnail, so it will invoke either the “sendRequest()” or “displayImage()” functions, according to the value of the “loaded” flag. What this means is that the first time the user clicks on a thumbnail (loaded=false), all the big images will be fetched from the server by using a simple XML file as the containing structure for storing image names. Otherwise, if the pictures have been preloaded (loaded=true), the “displayImage()” function is called up, passing into it the ID of the <a> element that triggered the event, and only the image tied to the thumbnail will be displayed.

    As you might guess, you can use a database table for directly fetching the images instead of an XML file, or even dynamically build the path and name of the picture to be displayed. As I explained before, the script can be easily modified to fit particular needs.

    For the moment, I’ve demonstrated how thumbnails are created once the web document has been loaded, in addition to explaining the way that program flow is moved after checking whether all of the images have been properly preloaded. Considering this, the next step in the development of the application consists of having a quick look at the “sendRequest()” function, together with the definition for the XML file that stores the pertinent image names. So, scroll down the page to learn how this is done.

    More JavaScript Articles
    More By Alejandro Gervasio


       · 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek