Click here to Skip to main content
15,884,298 members
Articles / Web Development / HTML

Opening an image in a new closeable browser window

Rate me:
Please Sign up or sign in to vote.
2.94/5 (6 votes)
2 Sep 2006CPOL2 min read 78.7K   677   13   4
This article gives an example to beginner web developers about using JavaScript functions.

Introduction

This article describes how to open a picture in a popup window and close it with a single mouse click.

Concept

The idea behind the code is to programmatically create a new window with JavaScript, and also to write into it, with the simple document.write method. To understand this script, you must be able to understand basic HTML tags like html, body, a (anchor), and img.

The code

In the example, we will implement the main script only once, and we can use it any time on this page. So the first thing we have to do is to copy the following JavaScript block into the head of the HTML page:

JavaScript
function OpenNewWindow(bigurl, width, height)
{
    var newWindow = window.open("", "pictureViewer", 
        "location=no, directories=no, fullscreen=no, " + 
        "menubar=no, status=no, toolbar=no, width=" + 
        width + ", height=" + height + ", scrollbars=no");
    newWindow.document.writeln("<html>");
    newWindow.document.writeln("<body style='margin: 0 0 0 0;'>");
    newWindow.document.writeln("<a href='javascript:window.close();'>");
    newWindow.document.writeln("<img src='" + bigurl + 
       "' alt='Click to close' id='bigImage'/>");
    newWindow.document.writeln("</a>");
    newWindow.document.writeln("</body></html>");
    newWindow.document.close();
}

Of course, the script must be surrounded by <script> tags.

Let’s see how it works. The function has to be called with three parameters. The first one is the URL of the picture, what we would like to open, the second and the third are the dimensions of the picture.

In the first row of the function, a new window will be opened, with the specified height and width parameters. The window.open method also gets a name parameter (“pictureViewer”). If this parameter is not an empty string, all our pictures are opened in the same browser window that was opened first.

After opening a new window, the code will write a simple HTML page in it.

HTML
<html>
   <body style='margin: 0 0 0 0;'>
      <a href='javascript:window.close();'>
         <img src='xy.jpg' alt='Click to close' id='bigImage'/>
      </a>
   </body>
</html>

You can see that there is a small script in the anchor tag which can close the newly opened window. The name of the picture (xy.jpg) comes from the parameter value.

If you would like to use margins, you can alter the style attribute of the body tag.

Calling the script

In the main page, you must put an anchor tag to surround that content, which has to open the new window. For example, if you want to display a picture in a detailed view, you can use this:

HTML
<a href="#" onclick="OpenNewWindow('images/bigpicture.jpg', 500, 500); return true;">
   <img src="images/smallpicture.jpg" alt="Click..."/>
</a>

The big picture and the small picture image must exist at the specified position.

JavaScript running from local computer

Internet Explorer usually prohibits script running from the local computer. So you should avoid testing the attached HTML code with Internet Explorer from your local computer or you must always click on the “Allow blocked content” menu in the yellow bar.

Hungarian version

The Hungarian version of this article is accessible here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader GSGroup
Hungary Hungary

Comments and Discussions

 
GeneralCorrection and suggestion (code sample) [modified] Pin
Red Feet28-Aug-06 5:48
Red Feet28-Aug-06 5:48 
AnswerRe: Correction and suggestion (code sample) Pin
István Kovács (HU)28-Aug-06 23:12
István Kovács (HU)28-Aug-06 23:12 
Hello,

I looked after the "margin" css keyword on W3c.org. The margin is written without an "S" at the end. You can see it here: http://www.w3.org/TR/CSS1#margin

The image toolbar and the changed anchor seems to be a good idea, thanks for these! Wink | ;)
GeneralRe: Correction and suggestion (code sample) Pin
Geoff Middleton2-Sep-06 10:58
Geoff Middleton2-Sep-06 10:58 
AnswerRe: Correction and suggestion (code sample) Pin
István Kovács (HU)2-Sep-06 15:28
István Kovács (HU)2-Sep-06 15:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.