Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2011
    Location
    Raleigh, NC
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Closing JPEG File Using HTML

    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:

    Code:
    <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>
    </tr>
    
    <tr>
    <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>
    </tr>
    
    <tr>
    <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>
    </tr>
    
    </table>
    Any help would be greatly appreciated!!

    Thanks,

    Brett

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    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.
    Code:
    <!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>
            <title></title>
            <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
                }
            </style>
            <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){
                    if(newWin){
                        newWin.document.getElementById('imgEnlarge').src = picO[num].src;
                        newWin.document.getElementById('caption').innerHTML = picData[num][1];
                        newWin.focus();
                    } 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>');
                    }
                }
                window.onload=function(){
                    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;
                        imgO.onclick=function(){
                            showEnlargement(this.num);
                        }
                        liO.appendChild(imgO);
                        ulThumbsO.appendChild(liO);
                    }
                }
                //-------------------------------------------
                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;
                }
            </script>
        </head>
        <body>
            <!-- 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];
                    document.getElementById('preloadedPics').appendChild(newImg);
                }
            </script>
            <!-- --------------------End of image preloads ------------------ -->
            <p>Click on a thumbnail</p>
            <ul id="ulThumbs"></ul>
        </body>
    </html>
    Last edited by bullant; 05-03-2011 at 09:39 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •