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. |