« Validating multiple emails in one field-- server-side | Main | Safari browser now available for Windows »

JavaScript for centering a popup window to the screen

Ever wondered how to get your popup windows to center themselves on screen instead of appearing wherever your browser wants to put them? Just measure window.screen.width and window.screen.height. Divide each of them in half to get the center point of your screen. Then subtract from those numbers half of the width and height of your popup window. The result is the X,Y coordinates of where you should place the upper left corner of your popup to make it centered:
function newWindow(windowName, URL, width, height, scrolling) {
	width = width || 400;
	height = height || 360;
	scrolling = scrolling || 0;
	
	var topX = (window.screen.width / 2) - ( width / 2);
	var topY = (window.screen.height / 2) - ( height / 2);
	
	window.open(URL, windowName, 'width=' + width + ',height=' + height +
 ',location=no,resizable=yes,scrollbars=' + scrolling + 
',screenX=' + topX + ',screenY=' + topY);
}

Comments (1)

Nice:)

Post a comment