Debt Consolidation - Apartment Budapest - Novated lease - Turquoise Rings - Free Articles Directory About Cancer

PDA

View Full Version : javascript center?


izlik
Feb 24th 2009, 4:17 pm
Hello. the javascript code bellow is for a popup window. currently the popup appears at the top left of my screen, how can i make it center itself once it popups?

<SCRIPT TYPE="text/javascript">
<!--
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=400,height=200,scrollbars=yes');
return false;
}
//-->
</SCRIPT>

'<A HREF="pop.php?file=$file" onClick="return popup(this)">' . $file . '</A>'

lp1051
Feb 25th 2009, 5:41 pm
Hi izlik,

try method moveTo(Xcoord, Ycoord). For that you need to hold the opened window in some variable.
E.g.
var myWin = window.open(href, windowname, 'width=400,height=200,scrollbars=yes');
myWin.moveTo(200, 200);
If you need to center it based on viewport size and window size, just count X and Y coordinates before calling moveTo.

astupidname
Feb 26th 2009, 6:48 am
<html>
<head>

<script type="text/javascript">

function popup(mylink, windowname,width,height) {
if (! window.focus) {
return true;
}
var href,
l = (screen.width / 2) - (width / 2),
t = (screen.height / 2) - (height / 2);
if (typeof(mylink) == 'string') {
href=mylink;
} else {
href=mylink.href;
}
window.open(href, windowname,'width=' + width + ',height=' + height + ',left=' + l + ',top=' + t + ',scrollbars=yes');
return false;
}

</script>
</head>
<body>
<a href="http://www.example.com" onclick="return popup(this,'astupidWindow',400,200);">Go google somebody</a>
</body>
</html>