How to Center Popups

Much abused but not always bad.

Shop for web design and development books here.

Popup windows are not used much anymore, because most people block them. However, they are still handy in some cases and, if used, should always be centered. Few things are more irritating than popups that open crammed in the corner. The following is a commonly-used script for automatic popup centering.

<script type="text/javascript">
function popup(mylink, windowname)
{
if (! window.focus)return true;
 var wLeft = (screen.width - 640) / 2;
 var wTop = (screen.height - 480) / 2;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=640,height=480,left=' + wLeft + ',top=' + wTop + ',resizable=yes,scrollbars=yes');
return false;
}
</script>

Change the desired size in both places, then call it with something like this:

<input type=button value="My Pop" onclick="popup('http://some.com/some.html','My Pop')">

Temporarily allow popups and .

There are many other free popup scripts on the web. I like this one because it’s simple and just right for most purposes.

Leave a Reply

Your email address will not be published.