Tuesday, October 16, 2007

Javascript open centered popup window

How to open a centered pop-up window in Javascript


Here is a Javascript function that allows you to open a centered window, no matter what resolution the visitor uses.

function OpenCenteredWindow( url, width, height )
{
var left = Math.floor( (screen.width - width) / 2);
var top = Math.floor( (screen.height - height) / 2);
var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
winParms += ",toolbar=no,status=no,scrollbars=yes,menubar=no,resizable=no,location=no,directories=no";
var win = window.open(url, '', winParms);
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
return win;
}

There are three parameters:
  • url - the address you want to open in the new window
  • width - the width of the new popup window
  • height - the height of the new centered window
Also you can set what parameters the new window will have: toolbar, status, scrollbars, menubar, resizable, location, directories.

No comments: