Resize a Popup to Fit an Image's Size


/ Published in: JavaScript
Save to your folder(s)

Resize a Popup to Fit an Image's Size
By Peter Todorov
February 10th 2003
Reader Rating: 9.1

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.


Copy this code and paste it in your HTML
  1. First of all you'll need one main HTML [1] page that will contain links to the full-sized pictures in your gallery:
  2.  
  3. <HTML>
  4. <HEAD>
  5. <TITLE>The Image Gallery</TITLE>
  6. <script language="Javascript [2]">
  7. function PopupPic(sPicURL) {
  8. window.open( "popup.htm?"+sPicURL, "",
  9. "resizable=1,HEIGHT=200,WIDTH=200");
  10. }
  11. </script>
  12. </HEAD>
  13. <BODY bgcolor="#FFFFFF">
  14. <a href="javascript:PopupPic('Image1.gif [3]')">Image 1</a><br>
  15. <a href="javascript:PopupPic('Image2.gif')">Image 2</a><br>
  16. <a href="javascript:PopupPic('Image3.gif')">Image 3</a><br>
  17. </BODY>
  18. </HTML>
  19.  
  20. 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.
  21.  
  22. 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:
  23.  
  24. window.open( "popup.htm?"+sPicURL, "",
  25. "resizable=1,HEIGHT=200,WIDTH=200");
  26.  
  27. Now, take a look at the code of the popup.htm page:
  28.  
  29. <HTML>
  30. <HEAD>
  31. <TITLE>Fit the Pic Script</TITLE>
  32. <script language='javascript'>
  33. var arrTemp=self.location.href.split("?");
  34. var picUrl = (arrTemp.length>0)?arrTemp[1]:"";
  35. var NS = (navigator.appName=="Netscape")?true:false;
  36.  
  37. function FitPic() {
  38. iWidth = (NS)?window.innerWidth:document.body.clientWidth;
  39. iHeight = (NS)?window.innerHeight:document.body.clientHeight;
  40. iWidth = document.images[0].width - iWidth;
  41. iHeight = document.images[0].height - iHeight;
  42. window.resizeBy(iWidth, iHeight);
  43. self.focus();
  44. };
  45. </script>
  46. </HEAD>
  47. <BODY bgcolor="#000000" onload='FitPic();' topmargin="0"
  48. marginheight="0" leftmargin="0" marginwidth="0">
  49. <script language='javascript'>
  50. document.write( "<img src='" + picUrl + "' border=0>" );
  51. </script>
  52. </BODY>
  53. </HTML>
  54.  
  55. 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:
  56.  
  57. var arrTemp=self.location.href.split("?");
  58. var picUrl = (arrTemp[1].length>0)?arrTemp[1]:"";
  59. var NS = (navigator.appName=="Netscape")?true:false;
  60.  
  61. First, we get the page URL string and split it by the "?" character. The result of this split is held in the arrTemp array [4] variable.
  62.  
  63. 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.
  64.  
  65. 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.
  66.  
  67. After we have the PicURL variable, we can easily write the image into the document's body using document.write:
  68.  
  69. document.write( "<img src='" + picUrl + "' border=0>" );
  70.  
  71. 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:
  72.  
  73. iWidth = document.images[0].width - iWidth;
  74. iHeight = document.images[0].height - iHeight;
  75. window.resizeBy(iWidth, iHeight);
  76.  
  77. 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 [5] with document.images[0] -- this way, we can get image's width and height.
  78.  
  79. 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.
  80.  
  81. 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.
  82.  
  83. By definition, this function resizes the current browser window by a certain amount. It takes 2 parameters: window.resizeBy(X, Y):
  84.  
  85. * X - The number of pixels to grow the window horizontally
  86. * Y - The number of pixels to grow the window vertically
  87.  
  88. The following line shrinks the window's width by 10px and extends its height by 13px:
  89.  
  90. window.resizeBy(-10, +13);
  91.  
  92. The final line in our FitPic() function puts the focus on the popup.
  93.  
  94. 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.

URL: http://www.sitepoint.com/print/1022

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.