Forum Moderators: open
This code centers the popup window over the place where the user clicked to invoke the popup window, except it makes sure that the window still fits on the screen if centering over the mouseclick would cause part of the window to appear off-screen. Actually, it's more correct to say it *usually* works, doesn't work every time or with every browser, and I don't remember in which situations it doesn't work. For me it's a "good enough" solution.
By the way, if you don't use *any* special code at all, then different browsers put the popup in different places! You seemed to assume that the window goes to only one spot. That's because of the browser you're using -- switch browsers and the window can go to a different location.
<script language="JavaScript"><!--
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var xMouse = 0;
var yMouse = 0;
function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
xMouse = event.screenX //event.clientX + document.body.scrollLeft;
yMouse = event.screenY //event.clientY + document.body.scrollTop;
}
else { // grab the x-y pos.s if browser isn't IE
xMouse = e.screenX;
yMouse = e.screenY;
}
return true;
}
//-----------------------------------------------
function popWindow(filename,theWidth,theHeight)
{
if (theHeight > screen.availHeight) {theHeight = screen.availHeight-60;}
theLeft = xMouse-(theWidth/2);
if (theLeft < 0) theLeft = 0; // prevents window from going too far left
if (theLeft + theWidth > screen.availWidth) theLeft = xMouse - theWidth; // prevents window from going too far right
theTop = yMouse-(theHeight/2);
if (theTop < 0) {theTop = 0} // prevents window from going too far up
if (theTop + theHeight > screen.availHeight) {theTop = screen.availHeight - theHeight-40;}; // prevents window from going too far down
features= "width=" +theWidth+ ",height=" +theHeight+ ",left=" +theLeft+ ",top=" +theTop;
popup=window.open("","thepopup",features);
popup.document.write("<HTML><body bgcolor='white' onblur=window.close() onclick=window.close()><P style='font:12px Arial; text-align:center'><IMG src='" +filename+ "'><BR>Click picture to close</P></body></HTML>");
}
//--></script>