Fit Popup window Size to the Image Size using JavaScript
There may be requirements where we need to display the full image in a popup window when we click a thumbnail image in aspx page. By default, the popup window will display the full image with lots of white spaces on the sides of the window. To make the image to exactly fit the popup window, we need to set the popup dimension to the image dimension.
The following code snippet will help us to fit the popup window dimension to the image dimension using JavaScript.
<script language="javascript">
function FitImageToWindow()
{
var width = document.images[0].width;
var height = document.images[0].height;
window.resizeTo(width,height);
}
</script>
<img src="new.jpg" onload="FitImageToWindow()" />
In the above code, the popup window is resized using window.resizeTo() method by specifying the image width and height as the popup window's dimension through the javascript method FitImageToWindow(). This javascript method will be called on onload event of the event.
|