The following javascript resizes a popup browser to the size of the image displayed in the browser. It has worked fine in IE6 and other browsers, but in IE7 it causes the width to be much too small, cutting off the image in the popup.
It appears that IE7 is treating "document.body.clientWidth-document.images[0].width" differently than in IE6. Anyone know how to easily remedy this
Here's the script....
<script language="JavaScript">
//-->
// Set the horizontal and vertical position for the popup
PositionX = 5;
PositionY = 5;
// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)
defaultWidth = 720;
defaultHeight = 520;
// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows
var AutoClose = false;
// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape") 1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1) 1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function popImage(imageURL,imageTitle){
if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');
writeln('<META HTTP-EQUIV="imagetoolbar" CONTENT="no">');
writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape") 1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1) 1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
writeln('width=100-(document.body.clientWidth-document.images[0].width);');
writeln('height=100-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
close();
}}
</script>
<script language="JavaScript" type="text/javascript" >
<!--
document.oncontextmenu = function(){return false}
if(document.layers) {
window.captureEvents(Event.MOUSEDOWN);
window.onmousedown = function(e){
if(e.target==document)return false;
}
}
else {
document.onmousedown = function(){return false}
}
//-->
</script>
HTML output ends up looking like this...
<html>
<head>
<title>Loading...</title>
<style>body{margin:0px;}</style>
<META HTTP-EQUIV="imagetoolbar" CONTENT="no">
<script>
var isNN,isIE;
if (parseInt(navigator.appVersion.charAt(0))>=4){
isNN=(navigator.appName=="Netscape") 1:0;
isIE=(navigator.appName.indexOf("Microsoft")!=-1) 1:0;}
function reSizeToImage(){
if (isIE){
window.resizeTo(100,100);
width=100-(document.body.clientWidth-document.images[0].width);
height=100-(document.body.clientHeight-document.images[0].height);
window.resizeTo(width,height);}
if (isNN){
window.innerWidth=document.images["George"].width;
window.innerHeight=document.images["George"].height;}}
function doTitle(){document.title="TheDomain.com";}
</script>
</head>
<body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">
<img name="George" src=theimage.jpg style="display:block"></body></html>

Javascript Popup/Image Width issue
John.Doe
I know that this is an old thread, but I have just run into similar problems.
My script looks like this:
function myPopup() {
window.open("mediaplayer/index.html","winPlayer","height=100,innerHeight=100,width=200,innerWidth=200,status=no,screenX=192,screenY=192");
}
It's not as advanced as yours, but im having problems with the width in IE7. what do I need to change to make it work
-Frost
James_Steven
I found a simple fix, though I do not know why this script works in IE-7 and the previous IE-6 compliant version does not.
Replace your existing script above with the following script code and it will function perfectlyin IE-7...
<script type="text/javascript" language="JAVASCRIPT_VERSION">
PositionX = 10;
PositionY = 10;
defaultWidth = 600;
defaultHeight = 400;
var AutoClose = true;
function popImage(imageURL,imageTitle){
var imgWin = window.open('','_blank','scrollbars=no,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
if( !imgWin ) { return true; } //popup blockers should not cause errors
imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
'function resizeWinTo() {\n'+
'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
'var oH = document.images[0].height, oW = document.images[0].width;\n'+
'if( !oH || window.doneAlready ) { return; }\n'+ //in case images are disabled
'window.doneAlready = true;\n'+ //for Safari and Opera
'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\n'+
'var scW = screen.availWidth screen.availWidth : screen.width;\n'+
'var scH = screen.availHeight screen.availHeight : screen.height;\n'+
'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
'}\n'+
'<\/script>'+
'<\/head><body onload="resizeWinTo();"'+(AutoClose ' onblur="self.close();"':'')+'>'+
(document.layers ('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;display:table;">'))+
'<img src='+imageURL+' alt="Loading image ..." title="" onload="resizeWinTo();">'+
(document.layers '<\/layer>':'<\/div>')+'<\/body><\/html>');
imgWin.document.close();
}
</script>
craigory
"But can I be so rude as to ask if it can be changed to allow the popup to open on the upper left and to click anywhere and have the popup disapear without having to close it, like the original."
Having no javascript knowledge at all, I compared my original script and the new script and saw that if you change the line
<img src='+imageURL+' alt="Loading image ..." title="" onload="resizeWinTo();">'+
to the following (one line)
'<img src='+imageURL+' onMouseOver="this.style.cursor=\'hand\'" onMouseOut="this.style.cursor=\'default\'" style="display:block" onclick="window.close()" alt="Click to Close Image." title="" onload="resizeWinTo();">'+
that you get the back the autoclose.
The whole script thus becomes:
PositionX = 10;
PositionY = 10;
// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)
defaultWidth = 600;
defaultHeight = 400;
// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows
var AutoClose = true;
function popImage(imageURL,imageTitle){
var imgWin = window.open('','_blank','scrollbars=no,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
//popup blockers should not cause errors
if( !imgWin ) { return true; }
imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
'function resizeWinTo() {\n'+
'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
'var oH = document.images[0].height, oW = document.images[0].width;\n'+
//in case images are disabled
'if( !oH || window.doneAlready ) { return; }\n'+
//for Safari and Opera
'window.doneAlready = true;\n'+
'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
'<div style="position:absolute;left:0px;top:0px;display:table;">'))+
'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\n'+
'var scW = screen.availWidth screen.availWidth : screen.width;\n'+
'var scH = screen.availHeight screen.availHeight : screen.height;\n'+
'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
'}\n'+
'<\/script>'+
'<\/head><body onload="resizeWinTo();"'+(AutoClose ' onblur="self.close();"':'')+'>'+
(document.layers ('<layer left="0" top="0">')
'<img src='+imageURL+' onMouseOver="this.style.cursor=\'hand\'" onMouseOut="this.style.cursor=\'default\'" style="display:block" onclick="window.close()" alt="Click to Close Image." title="" onload="resizeWinTo();">'+
(document.layers '<\/layer>':'<\/div>')+'<\/body><\/html>');
imgWin.document.close();
}
Victor Leung
After further investigation the issue seems to be that IE-7 ignores the width command and uses the height command as the width command.
The resized popup image is always a square, with the same dimension set for the width (the incorrect width that is) set to the exact same size as the height.
I do not know enough of javascript programming to narrow the problem any further.
If anyone here can help, please post!
DeanoCalver
Would just like to add a word of thanks. I was using similar code and am not crash hot at Java; I would have struggled greatly to fix it up.
Champion effort, keep it up.
- Pufinstuff
Marcello2
Wow, I was having the same problem with IE 7 and used the EXACT same script the original poster did. I got it off of a web site a long time ago, I'm not javascript savy :).
Thanks for the new script. But can I be so rude as to ask if it can be changed to allow the popup to open on the upper left and to click anywhere and have the popup disapear without having to close it, like the original. I have many pictures to view. I tried changing the position x and y... but no change. It opens right in the center of the page.
Thanks again. So glad I came upon this site.
Alex_UTL
I have the same exact issue...
The script I am using is very close to yours, just a few variables different in the default size..
The popup image is about 30 to 35% to narrow and it cuts off the image.
So far I can not locate what is wrong in the way IE-7 is handing the script to isolate the error.
Any input is welcome!