HomeASP HostingWeb DesignRegister DomainSupportAbout Us

FAQ
Support :: FAQ


Resize a Popup to Fit an Image's Size in JavaScript
If you're a Webmaster who has to deal with image galleries, then make a note of this clever script. It gives you a solution to a very common problem -- how to resize a popup window to fit the image sizes displayed in it. The script works in NS 4/5/6/7 and IE 4/5/6.

First of all you'll need one main HTML page that will contain links to the full-sized pictures in your gallery:

<HTML>
 <HEAD>
   <TITLE>The Image Gallery</TITLE>
   <script language="Javascript">
   function PopupPic(sPicURL) {
     window.open( "popup.htm?"+sPicURL, "",  
     "resizable=1,HEIGHT=200,WIDTH=200");
   }
   </script>
 </HEAD>
<BODY bgcolor="#FFFFFF">
   <a href="javascript:PopupPic('Image1.gif')">Image 1</a><br>
   <a href="javascript:PopupPic('Image2.gif')">Image 2</a><br>
   <a href="javascript:PopupPic('Image3.gif')">Image 3</a><br>
</BODY>
</HTML>

Let's study the code above a little. We have a straightforward HTML page that contains a couple of links, and defines a simple Javascript function: PopupPic(). By clicking on any of the links on this page, you'll call the PopupPic() function. This function is really simple: the only thing it does is create a popup browser window, and load the popup.htm page in it.

The trick is that we pass the image's URL (relative to the image gallery Web page location) in the query string when the popup is created:

window.open( "popup.htm?"+sPicURL, "",  
"resizable=1,HEIGHT=200,WIDTH=200");

Now, take a look at the code of the popup.htm page:

<HTML>
<HEAD>
 <TITLE>Fit the Pic Script</TITLE>
 <script language='javascript'>
   var arrTemp=self.location.href.split("?");
   var picUrl = (arrTemp.length>0)?arrTemp[1]:"";
   var NS = (navigator.appName=="Netscape")?true:false;

     function FitPic() {
       iWidth = (NS)?window.innerWidth:document.body.clientWidth;
       iHeight = (NS)?window.innerHeight:document.body.clientHeight;
       iWidth = document.images[0].width - iWidth;
       iHeight = document.images[0].height - iHeight;
       window.resizeBy(iWidth, iHeight);
       self.focus();
     };
 </script>
</HEAD>
<BODY bgcolor="#000000" onload='FitPic();' topmargin="0"  
marginheight="0" leftmargin="0" marginwidth="0">
 <script language='javascript'>
 document.write( "<img src='" + picUrl + "' border=0>" );
 </script>
</BODY>
</HTML>

The first thing that should grab our attention is the fact that we're using Javascript code, which is directly executed while the page loads:

var arrTemp=self.location.href.split("?");
var picUrl = (arrTemp[1].length>0)?arrTemp[1]:"";
var NS = (navigator.appName=="Netscape")?true:false;

First, we get the page URL string and split it by the "?" character. The result of this split is held in the arrTemp array variable.

On the second line we check if the second element of our temp array -- arrTemp[1] -- has length greater than 0, and if this is true, we assign the value of this second array element to the picURL variable.

On the third line we assign true to the NS variable if the browser is Netscape, otherwise we assign false. As you may already have guessed, the PicURL variable contains the relative URL of the picture that will be displayed in the popup.htm page.

After we have the PicURL variable, we can easily write the image into the document's body using document.write:

document.write( "<img src='" + picUrl + "' border=0>" );

After the page is completely loaded into the browser, the Load event is triggered and because we use onLoad event handler, the function FitPic() is called. The first 2 lines of this function find the browser's window width and height (depending on the browser). The next 3 lines however, are the most important lines in this function. Let's have a good look at them:

iWidth = document.images[0].width - iWidth;
iHeight = document.images[0].height - iHeight;
window.resizeBy(iWidth, iHeight);

After the page is fully loaded we can access the document's image collection, thus gaining access to the image properties. Because we have only one image on our page, and because the images collection is zero-based, the image is accessible with document.images[0] -- this way, we can get image's width and height.

Then we subtract the initial browser width from the actual image width -- the result is a number by which the browser width has to be resized. We do the same with the initial browser height and the actual image height to ascertain the number of pixels by which we should resize the browser height in order to fit the picture.

The 3rd line is the actual resizing done by JavaScript's built in resizeBy() function. If you aren't familiar with the resizeBy() function, here's a short explanation.

By definition, this function resizes the current browser window by a certain amount. It takes 2 parameters: window.resizeBy(X, Y):

  • X - The number of pixels to grow the window horizontally
  • Y - The number of pixels to grow the window vertically

The following line shrinks the window's width by 10px and extends its height by 13px:

window.resizeBy(-10, +13);

The final line in our FitPic() function puts the focus on the popup.

So, to summarize how the script works, we pass the image-relative URL to the popup.htm (popup.htm?Images/Image1.gif), then we write the image tag into the body of the page with document.write, and when the page is loaded, we call FitPic(), which resizes the browser window to the image size.

To see our script in action, click here

© Copyright 2001-2004 Art Branch Inc.