upgrade from debian wheezy to squeeze | feed generator php Script | php current date


Center popup window on the Screen with Javascript

Javascript Window open() Method opens a popup window. How to center this window on the screen?

A solution could be the following function: CenterWindow

	 * Opens window screen centered.
	 * @param windowWidth the window width in pixels (integer)
	 * @param windowHeight the window height in pixels (integer)
	 * @param windowOuterHeight the window outer height in pixels (integer)
	 * @param url the url to open
	 * @param wname the name of the window
	 * @param features the features except width and height (status, toolbar, location, menubar, directories, resizable, scrollbars)
	function CenterWindow(windowWidth, windowHeight, windowOuterHeight, url, wname, features) {
		var centerLeft = parseInt((window.screen.availWidth - windowWidth) / 2);
		var centerTop = parseInt(((window.screen.availHeight - windowHeight) / 2) - windowOuterHeight);

		var misc_features;
		if (features) {
			misc_features = ', ' + features;
		else {
			misc_features = ', status=no, location=no, scrollbars=yes, resizable=yes';
		var windowFeatures = 'width=' + windowWidth + ',height=' + windowHeight + ',left=' + centerLeft + ',top=' + centerTop + misc_features;
		var win = window.open(url, wname, windowFeatures);
		return win;


Here is an example

<a href="javascript:void(0)"
   onclick="CenterWindow(1000,800,50,'http://www.pontikis.net','demo_win');">Click here</a>


Click here

Your comments are welcomed!

Sign-up for our free email newsletter. Get updates when new tutorials and tips are published. You can unsubscribe anytime with a click.

Your comments are welcomed!

This site actively encourages commenting on any post. Comments are not pre-moderated, but this community does not tolerate direct or indirect attacks, name-calling or insults. Please, read terms of use and Comment Policy at privacy policy.

More tips