View Full Version : Closing JPEG File Using HTML

05-03-2011, 04:03 AM
This may sound a little strange and maybe it's not possible, but I have a simple thumbnail gallery set up on my VERY simple site. When the user clicks on the thumbnail image (jpeg), it opens up a new window with the full size version of the image.

Now the question... Is there a way to put a "Close" button or link on the new window that opens up for the full image? Here is a small snippet of the coding I have set up so far:

<table width="735" border="0" cellspacing="30px">
<tr align="center">

<td><a href="images/spacecolor.jpg" target="_blank"><img src="images/spacecolor.jpg" alt="Colors of Space" width="200" height="150" title="Colors of Space" /></a></td>

<td><a href="images/commonsense2.jpg" target="_blank"><img src="images/commonsense2.jpg" alt="Common Sense" width="200" height="150" title="Common Sense" /></a></td>

<td><a href="images/deadfrog.jpg" target="_blank"><img src="images/deadfrog.jpg" alt="Dead Frog" width="200" height="150" title="Dead Frog" /></a></td>

<td><a href="images/swordplay.jpg" target="_blank"><img src="images/swordplay.jpg" alt="Swordplay" width="200" height="150" title="Sword Play" /></a></td>

<td><a href="images/eyenumbing.jpg" target="_blank"><img src="images/eyenumbing.jpg" alt="Eye Numbing" width="200" height="150" title="Eye Numbing" /></a></td>

<td><a href="images/habitat.jpg" target="_blank"><img src="images/habitat.jpg" alt="Habitat for Humanity" width="200" height="150" title="Habitat for Humanity" /></a></td></tr>

<tr align="center">
<td><a href="images/texturedstar.jpg" target="_blank"><img src="images/texturedstar.jpg" alt="Textured Star" width="200" height="150" title="Textured Star" /></a></td>

<td><a href="images/winelabel2.jpg" target="_blank"><img src="images/winelabel2.jpg" alt="Wine Label" width="100" height="200" title="Wine Label" /></a></td>

<td><a href="images/colorful.jpg" target="_blank"><img src="images/colorful.jpg" alt="Colors" width="200" height="150" title="Colorful" /></a></td>

<td><a href="images/beach.jpg" target="_blank"><img src="images/beach.jpg" alt="Beach" width="200" height="150" title="Relaxation" /></a></td>

<td><a href="images/piet_mondrian.jpg" target="_blank"><img src="images/piet_mondrian.jpg" alt="Piet Mondrian" width="200" height="150" title="Piet Mondrian" /></a></td>


Any help would be greatly appreciated!!



05-03-2011, 08:01 AM
Now in the current way you are doing it. You would either need to put each image on it's own html page (not the best way, would take forever) or look into using something like Lightbox or an alternative to it. Opening just the image like you are doing now, it isn't possible to do what you want without javascript.

05-03-2011, 09:32 AM
Maybe use this demo as a guide.

The thumbnails are created dynamically according to the images in the picData array.

When you click on a thumbnail, its enlargement and caprion will appear in a new window. If you then click another thumbnail, it's enlargement and caption will replace the current enlargement in the same new window.

The enlargement window has a close button on it. The enlargement window is set to 800 x 800 in winParams

This works in my IE9 and FF4.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">
#ulThumbs {
clear: both;
list-style-type: none
#ulThumbs li {
margin: 0px 10px 0px 0px;
padding: 0px 0px 0px 0px;
display: inline
<script type="text/javascript">
var picData = [
['num1.jpg','description 1'],
['num2.jpg','description 2'],
['num3.jpg','description 3'],
['num4.jpg','description 4']
var thumbMaxWidth = 100;
var thumbMaxHeight = 100;
var newWin = false;
function showEnlargement(num){
newWin.document.getElementById('imgEnlarge').src = picO[num].src;
newWin.document.getElementById('caption').innerHTML = picData[num][1];
} else {
winParams = 'width=800,height=800,toolbar=no,location=no';
newWin = window.open('','',winParams);
newWin.document.write('<div><img id="imgEnlarge" src="'+picO[num].src+'" alt="" /></div>');
newWin.document.write('<div id="caption">'+picData[num][1]+'</div>');
newWin.document.write('<div><button onclick="self.close();opener.newWin=false;">Close this window</button></div>');
enlargeContainerO = document.getElementById('enlargeContainer');
imgEnlargeO = document.getElementById('imgEnlarge');
descContainerO = document.getElementById('descContainer');
//load the images into an Image object
picO = new Array();
for(i=0; i < picData.length; i++){
picO[i] = new Image();
picO[i].src = picData[i][0];
//create the thumbnails
var ulThumbsO = document.getElementById('ulThumbs');
var thumbDims = new Array();
for(i=0; i < picO.length; i++){
liO = document.createElement('li');
imgO = document.createElement('img');
imgO.src = picO[i].src;
thumbDims = calcNewDimensions(picO[i].width, picO[i].height, thumbMaxWidth, thumbMaxHeight);
imgO.width = thumbDims['width'];
imgO.height = thumbDims['height'];
imgO.num = i;
function calcNewDimensions(width, height, maxWidth, maxHeight){
newDims = new Array();
var xRatio = maxWidth / width;
var yRatio = maxHeight / height;
//calculate the new width and height
if(width <= maxWidth && height <= maxHeight) { //image does not need resizing
newDims["width"] = width;
newDims["height"] = height;
} else if(xRatio * height < maxHeight) {
newDims["height"] = Math.round(xRatio * height);
newDims["width"] = maxWidth;
} else {
newDims["width"] = Math.round(yRatio * width);
newDims["height"] = maxHeight;
return newDims;
<!-- preload the images so we can use their actual width and height property
to scale the thumbnails -->
<div id="preloadedPics" style="display: none"></div>
<script type="text/javascript">
for(i=0; i < picData.length; i++){
var newImg = document.createElement('img');
newImg.src = picData[i][0];
newImg.alt = picData[i][1];
<!-- --------------------End of image preloads ------------------ -->
<p>Click on a thumbnail</p>
<ul id="ulThumbs"></ul>