Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1316 How do I center a popup window?

You are here: irt.org | FAQ | JavaScript | Window | Q1316 [ previous next ]

Try:

<script language="JavaScript"><!--
var w = 480, h = 340;

if (document.all || document.layers) {
   w = screen.availWidth;
   h = screen.availHeight;
}

var popW = 300, popH = 200;

var leftPos = (w-popW)/2, topPos = (h-popH)/2;

window.open('page,html','popup','width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos);
//--></script>

If you want to center the popup in the browser window use this instead of screen.availWidth and screen.availHeight:

<script language="JavaScript"><!--
var w = 480, h = 340;

if (document.all) {
   /* the following is only available after onLoad */
   w = document.body.clientWidth;
   h = document.body.clientHeight;
}
else if (document.layers) {
   w = window.innerWidth;
   h = window.innerHeight;
}

var popW = 300, popH = 200;

var leftPos = (w-popW)/2, topPos = (h-popH)/2;

window.open('page,html','popup','width=' + popW + ',height='+popH+',top='+topPos+','left='+leftPos);
//--></script>

©2018 Martin Webb