JavaScript
  Home arrow JavaScript arrow Page 4 - 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 
Sun Developer Network 
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 - Requesting data from the server: fetching images through an XML file


    (Page 4 of 5 )

    Since requesting files through XMLHttpRequest objects is a topic that I explained in some of my previous articles, I definitely won’t spend a long time explaining how this process is performed. Instead, I’ll briefly show the definition for the “sendRequest()” function, to give you a pretty clear idea of how images will be fetched from the server. Here is the function’s source code:

    function sendRequest(elemid,file){
        // check for existing requests
        if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
            xmlobj.abort();
        }
        try{
            // instantiate object for Mozilla, Nestcape, etc.
            xmlobj=new XMLHttpRequest();
        }
        catch(e){
            try{
                // instantiate object for Internet Explorer
                xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch(e){
                // Ajax is not supported by the browser
                xmlobj=null;
                return false;
            }
        }
        // assign state handler
        xmlobj.onreadystatechange=function(){
            stateChecker(elemid);
        }
        // open socket connection
        xmlobj.open('GET',file,true);
        // send request
        xmlobj.send(null);
    }

    As you may have guessed, I’ll use this handy function to read the contents of the XML file containing the names of the images that will be preloaded. Its incoming arguments are the ID of the <a> element that was clicked on (in short, the thumbnail accessed by the visitor), as well as the file to be fetched. For this specific example, I’ll define a basic “images.xml” file, which looks like this:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <pictures>
    <image>image1</image>
    <image>image2</image>
    <image>image3</image>
    <image>image4</image>
    </pictures>

    Studying the above file, it becomes clear that all the <image> nodes are containers for image names. I’ve purposely coded the file that way, because of the extreme ease provided by AJAX for fetching XML files –- and specifically node values -- by utilizing the “XMLResponse” property of XMLHttpRequest objects. You can also see that the sequence image1, image2…image N isn’t mandatory at all. It’s possible to specify different image names, as long as they meet the rules for well-formed XML files.

    Having defined the XML file, which stores image names, along with the function responsible for fetching it, we can move on to writing the next function of the application, “preloadImages()”, which not surprisingly does the work “behind the scenes” for preloading all the large images.

    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

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT