Home arrow JavaScript arrow Page 3 - Using JavaScript Click Interceptions in an Image Gallery
JAVASCRIPT

Using JavaScript Click Interceptions in an Image Gallery


If you’re a web developer with an intermediate level of experience building JavaScript applications, then you may have already used click interceptions in one form or another. Basically, as its name would suggest, this client-side method consists of “catching” a mouse click that occurs on a selected element of a web page to modify its default behavior. In this second part of a four-part series, you'll learn how to use click interceptions in an image gallery.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
November 12, 2008
TABLE OF CONTENTS:
  1. · Using JavaScript Click Interceptions in an Image Gallery
  2. · Building the basic structure of an image gallery
  3. · Using click interception to modify existing behavior
  4. · Listing the full source code of the improved image gallery

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials