Home arrow JavaScript arrow Page 4 - 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 - Listing the full source code of the improved image gallery
(Page 4 of 4 )

In consonance with the concepts I deployed in the section you just read, below I included the complete source code of this simple image gallery. As you saw before, it uses the click interception approach to improve its existing behavior.

Here's the corresponding code sample:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Example on building an image gallery using click interception</title>

<script language="javascript" type="text/javascript">

// define 'displayImage()' 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 'displayImage()' function when web page is loaded

window.onload=function(){

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

displayImages();

}

}

</script>

<style type="text/css">

body{

padding: 0;

margin: 0;

background: #fff;

}

h1{

font: bold 18pt Arial, Helvetica, sans-serif;

color: #000;

}

a:link,a:visited{

font: normal 10pt Arial, Helvetica, sans-serif;

color: #00f;

}

a:hover{

color: #f90;

}

#linkcontainer{

width: 300px;

padding: 5px;

background: #eee;

}

#imgcontainer{

width: 300px;

text-align: center;

}

</style>

</head>

<body>

<h1>Example on building an image gallery using click interception</h1>

<div id="linkcontainer">

<ul>

<li><a href="sampleimagea.gif" title="First sample image goes here">View first image</a></li>

<li><a href="sampleimageb.gif" title="Second sample image goes here">View second image</a></li>

<li><a href="sampleimagec.gif" title="Third sample image goes here">View third image</a></li>

</ul>

</div>

<div id="imgcontainer"></div>

</body>

</html>


Feel free to use all of the code samples shown in this article to improve your JavaScript click interception skills.

Final thoughts

In this second installment of the series, you hopefully learned how to utilize click interceptions to extend the existing functionality of a primitive image gallery. This example demonstrates how useful this approach can be when used in a real-word situation.

In the next tutorial, I'm going to show you how to use click interceptions to improve the behavior of a database-driven web application. This topic will be very educational and give you some useful knowledge, so don't miss the next part!


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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 2 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials