JavaScript centered popup window

August 15th, 2007 | Author: Chip

Today’s tutorial is about JavaScript and how to pop up a window in the center of the screen and focus on it. The trick is easy and the containing function is small. Here it is the JS code:

function window_popup(content, x, y, title) {
var thisWindow;
thisWindow = window.open(content,title,"width="+x+",height="+y+",scrollbars=auto,screenX=0,screenY=0,toolbar=no,location=no,menubar=no");
thisWindow.moveTo((screen.width-x)/2,(screen.height-y)/3);
thisWindow.window.focus();
}

And here is the caller:

<a href="#" onclick="window_popup('mypage.html', 400, 300, 'mypage')">Open My Page</a>

Remember to quote the page name and title, otherwise the script will generate an error. Popup window attributes can be changed in the second line of the function

scrollbars=auto,screenX=0,screenY=0,toolbar=no,location=no,menubar=no

depending on what is the role of the window.

9 comments

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv Enabled
Get Adobe Flash playerPlugin by wpburn.com wordpress themes