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:
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:
var arrTemp=self.location.href.split("?"); var picUrl = (arrTemp.length>0)?arrTemp:""; 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 — has length greater than 0, and if this is true, we assign the value of this second array element to the
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( "<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.width - iWidth; iHeight = document.images.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 — 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.
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:
- 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:
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.