JavaScript
  Home arrow JavaScript arrow Page 3 - Using JavaScript Click Interceptions in an...
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

Using JavaScript Click Interceptions in an Image Gallery
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-11-12

    Table of Contents:
  • Using JavaScript Click Interceptions in an Image Gallery
  • Building the basic structure of an image gallery
  • Using click interception to modify existing behavior
  • Listing the full source code of the improved image gallery

  • 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


    Using JavaScript Click Interceptions in an Image Gallery - Using click interception to modify existing behavior


    (Page 3 of 4 )

    As you might have guessed, changing the default behavior of each link that comprises the image gallery that you saw in the previous section is only a matter of defining a JavaScript function that first "intercepts" the mouse clicks, and then displays the sample images within the same web page, instead of opening a new window.

    Below I coded two brand new JavaScript functions which are responsible for performing the tasks mentioned. Here they are:


    // define 'displayImages()' function


    function displayImages(){

    var lnkcont=document.getElementById('linkcontainer');

    if(!lnkcont){return};

    var links=lnkcont.getElementsByTagName('a');

    if(!links){return};

    for(var i=0;i<links.length;i++){

    // use click interception to display images on the same web page

    links[i].onclick=function(){

    var imgcont=document.getElementById('imgcontainer');

    if(!imgcont){return};

    // if there is an existing image, remove it from the container

    if(imgcont.firstChild){

    imgcont.removeChild(imgcont.firstChild);

    }

    var img=document.createElement('img');

    img.setAttribute('src',this.href);

    img.setAttribute('alt',this.title);

    imgcont.appendChild(img);

    return false;

    }

    }

    }


    // call 'displayImages()' function when web page is loaded


    window.onload=function(){

    if(document.getElementById&&document.
    getElementsByTagName&&document.createElement){

    displayImages();

    }

    }


    Undoubtedly, in this specific case, the "displayImages()" JavaScript function defined above is the most relevant piece of this schema. It uses the click interception method to modify the default behavior of the links that comprise the sample image gallery. Because of this function, each time one of the links is  clicked, a new image will be displayed on a unique web page container, not surprisingly identified as "imagecontainer."

    As you can see, this process is performed by using some conventional DOM methods, so I assume that you shouldn't have major problems understanding how this works.

    And finally, the pertinent "displayImages()" JavaScript function must be called after loading the whole web page, so I simply attached it to a "window.onload" statement.

    Well, at this point I showed you how to use some click interceptions to extend the existing behavior of a basic image gallery. In this particular case, each time a descriptive link of the gallery is clicked on, it will trigger the pertinent click interception, and as a consequence, the image associated with it will be shown in the same web page container.

    However, and this is a direct consequence of building a JavaScript application that degrades gracefully, if scripting is disabled on the browser, the image gallery will still work seamlessly. Isn't this a good programming habit? You bet it is!

    Now that you understand how the previous "displayImages()" JavaScript function does its thing, it's time to show you the complete source code for this small client-side application that uses click interceptions to extend the existing behavior of a basic image gallery.

    However, as you may guess, this will be done in the course of the next section, so jump ahead and keep reading.

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this second chapter of the series, clicks interceptions are applied to extend...
     

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