Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1316 How do I center a popup window?

irt.org | Knowledge Base | JavaScript | Window | Q1316 [ previous next ]

Q1316 How do I center a popup window?

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>

Feedback on 'Q1316 How do I center a popup window?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.