JavaScript
  Home arrow JavaScript arrow Page 4 - Preloading Images with the DOM: A Function...
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: A Functional Image-Preloading Application
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2005-11-09

    Table of Contents:
  • Preloading Images with the DOM: A Functional Image-Preloading Application
  • Refreshing code: A quick look at the existing script functions
  • Building image containers: defining the “createImageContainer()” function
  • Displaying enlarged images: coding the “displayImage()” function
  • Gluing the pieces: turning the application into a functional AJAX-based preloader

  • 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: A Functional Image-Preloading Application - Displaying enlarged images: coding the “displayImage()” function


    (Page 4 of 5 )

    Fortunately, displaying zoomed-in pictures is a fairly easy thing to do. But in fact, I’m getting ahead of myself. So, let me first define the “displayImage()” function and then I’ll discuss its rationale. Below is its definition:

    function displayImage(elemid){
        var cdiv=document.getElementById('container');
        if(!cdiv){createImageContainer()};
        var newpic=pics[elemid];
        var oldpic=document.getElementById('largepic');
        if(!oldpic){return};
        oldpic.setAttribute('src',newpic.src);
    }

    In order to display a zoomed-in images, the function above will first check for the existence of the “container” <div> element that you just saw in the previous section. Since I want the function to show only one <div> for displaying images, the “createImageContainer()” function will be called up either the first time the user clicks on a thumbnail or when the containing <div> has been removed from the web document. The following line demonstrates this condition:

    if(!cdiv){createImageContainer()};

    Now, the process for swapping zoomed-in images is performed by utilizing the lines below:

    var newpic=pics[elemid];
    var oldpic=document.getElementById('largepic');
    if(!oldpic){return};
    oldpic.setAttribute('src',newpic.src);

    There are a couple of things that require our attention here. Notice the new image for display is stored in the “newpic” variable. Then, the old picture is replaced within the container with the new one by manipulating its “src” attribute, as follows:

    oldpic.setAttribute('src',newpic.src);

    Since all the images were previously preloaded into memory, the swapping process between pictures will be executed with practically no delays. As I explained before, this function looks a lot like a multiple image swapper.

    By this point, all the functions of the script have been defined and properly discussed. Keeping in mind that now the script is capable of preloading all the larger images, and displaying each of them with a subtle lag, it’s time to group all the functions into a single snippet and run it after the page has been loaded. Considering this, let’s jump straight into the next section and see how the complete application looks.

    More JavaScript Articles
    More By Alejandro Gervasio


       · In the last tutorial of this series, the remaining JavaScript functions that...
     

    JAVASCRIPT ARTICLES

    - 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...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






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