Click to See Complete Forum and Search --> : How to center a new window?


CalifNina
06-09-2003, 04:06 PM
I'm looking for some simple script to center a second window, versus whereever it just happens to come up and display.

Here is my current code:<SCRIPT TYPE="text/javascript"> //Opens new window "sounds"
function openWindow4()
{
window.open("sounds.html","myWindow","toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,copyhistory= yes,width=725,height=400")
}
</SCRIPT>All I'd like to do is center it on the screen; just do not know how to mathematically script and capture the information to figure out the centering. Also, I'm aware I could enter "top=0,left=0" and that would, for example, place it in the top-left position of the screen.

Please, asking for help in the needed scripting???

Khalid Ali
06-09-2003, 04:25 PM
Take a look at this link
http://68.145.35.86/skills/javascripts/CentralizedPopupWindow.html

David Harrison
06-09-2003, 04:25 PM
Well I don't know how to capture the screen width and height but I can help you with the maths.

After you have got the width and height store them in variables called "width" and "height" and I'll take you from there.

width=(width-725)/2;
height=(height-400)/2;

left=(width<0)?0:width;
top=(height<0)?0:width;

and that's it, well I didn't say that there was a lot of maths.

Charles
06-09-2003, 04:34 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<script type="text/javascript">
<!--
var x = Math.round ((screen.height - 400) / 2);
var y = Math.round ((screen.width - 725) / 2);
var geometry = 'height=400,width=725,scrollbars,status';
if (navigator.appName != 'Opera') geometry += ',top=' + x + ',left=' + y, ',screenX=' + x + ',screenY=', y;
// -->
</script>
<p><a href="http://www.w3.org/" onclick="window.open(this.href, 'child', geometry); return false">W3C</a></p>

CalifNina
06-09-2003, 05:03 PM
Thank you for the three different replies, will see if I can figure out what to do with it.