JavaScript
  Home arrow JavaScript arrow Page 3 - 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 - Building image containers: defining the “createImageContainer()” function


    (Page 3 of 5 )

    As you probably remember, after all the images have been preloaded into memory, it’s necessary to display the zoomed-in picture corresponding to the thumbnail that was clickedn. Considering that each image will require a wrapping element within the Web document, before displaying the pertinent picture, I’ll define a simple function, “createImageContainer()”, which will be responsible for creating a DIV element, in order to house the picture itself, together with the window-closing paragraph. Here is what this function looks like:

    function createImageContainer(){
        // create image container
        var cdiv=document.createElement('div');
        cdiv.setAttribute('id','container');
        var img=document.createElement('img');
        img.setAttribute('width','400');
        img.setAttribute('height','277');
        img.setAttribute('id','largepic');
        // create 'close' paragraph
        var p=document.createElement('p');
        p.appendChild(document.createTextNode('Close window[-]'));
        cdiv.appendChild(p);
        cdiv.appendChild(img);
        document.getElementsByTagName('body')[0].appendChild(cdiv);
        p.onclick=function(){
            this.parentNode.parentNode.removeChild(this.parentNode);
        }
    }

    Having defined the above function, let’s break down its code into pieces and see how it works. First, the function begins creating the containing <div> element, along with an <img> element. Then, it assigns some attributes to the elements just created, such as ID for the DIV, as well as “width” and “height” for the image. Surely, you’re wondering what the reason is for including an <img> element. If you’re used to working with old image swappers, the answer is pretty straightforward, since the script will switch between images by manipulating their “src” attribute. See how old techniques are merged with modern technology? 

    Now, returning to the remaining code, it’s fairly self-explanatory. As you can see, a <p> element is created on top of the containing <div>, in order to provide users with a simple control for removing the image they're seeing from the document. By studying the last lines of the function, the <div> element is removed through the following expression:

    p.onclick=function(){
        this.parentNode.parentNode.removeChild(this.parentNode);
    }

    Having illustrated the way that the script builds in the containing element for displaying large images, the next step will be defining a function that shows zoomed-in pictures. Thus, join me in the next section to find out how this process is carried out.

    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 2 hosted by Hostway
    Stay green...Green IT