Waqas Danish
Waqas Danish

Reputation: 111

Javascript - window.open() popup size issue

I am trying to display preloaded images in a newly created popup window. Here is the code:

HTML

<div id="hiddenImages" style="display: none;"></div>

<a href="BigImage1.png" onclick="OpenImage(this,event);" target="_blank"> <img src="SmallImage1.gif" width="196" height="130" border="0"></a>

<a href="BigImage2.png" onclick="OpenImage(this,event);" target="_blank"> <img src="SmallImage2.gif" width="196" height="130" border="0"></a>

JS

<script type="text/javascript">
preloadImages('BigImage1.png','BigImage2.png');
</script>

// PreLoadImages:

function preloadImages() {
     var hiddenDiv = document.getElementById('hiddenImages');
     var i;
     var img;

     for(i = 0; i < arguments.length; i++) {
          img = document.createElement('img');
          img.src = arguments[i];
          img.id = arguments[i];    
          img.alt = '';
          hiddenDiv.appendChild(img);
     }
}

// OpenImage:

function OpenImage(element,e)
{
  e.preventDefault();
  var big_image = element.getAttribute("href"); 
  var img = document.getElementById(big_image);

  var top = (screen.height/2)-(img.height/2);
  var left = (screen.width/2)-(img.width/2);


 var str = "\"" + "width=" + img.width +  ",height=" + img.height +  ",top=" + top +  ",left=" + left +  ",status=0, scrollbars=0, resizable=0" + "\"";
 alert(str);

 var myWindow = window.open(img.src , "win1", str); 


}

When I output 'str' variable in OpenImage() function, this code gives perfect dimensions of the big image. However, the size of popup window is always different than size of the image. IE stretches the popup window horizontally while Google Chrome stretches it vertically. In spite of the fact that both browsers show exactly the same dimensions for the image. And these dimensions of the image are actually being set as size of the pop up window.

Regards.

Upvotes: 0

Views: 1668

Answers (1)

Nin-ya
Nin-ya

Reputation: 252

Try this out, also try to rename your variable top, I think javascript read this as a keyword.

var str = "width=" + img.width +  ",height=" + img.height +  ",top=" + tops +  ",left=" + left +  ",status=0, scrollbars=0, resizable=0 ";

Upvotes: 1

Related Questions