Code Mamba

Code snippets

Opening and moving windows in Javascript

without comments

As many of you know we can open a window in javascript using:

window.open("some-page.htm","window-name","width=450,height=450");

The first argument is the URL of the new window, the second one is the window name and the third one is a list of comma-separated items.
Now how to move the new window? window.open returns an object. We have to catch that object and then use it. Here’s how to move the window to the top left corner:

newWin = window.open("some-page.htm","window-name","width=450,height=450");
newWin.moveTo(0,0);

Here’s how to center the window:

var width = 500;
var height = 600;
newWin = window.open("some-page.htm","window-name","width="+width+",height="+height);
newWin.moveTo(screen.width/2-width/2,screen.height/2-height/2);

Written by Wyand

September 14th, 2011 at 12:59 pm

Posted in JavaScript

Tagged with ,

Leave a Reply