View Full Version : window.open() with moveTo()

Feb 20th, 2012, 05:20 PM
I have the following generic function to open new windows:

function openWindows(url,name) {

variables for w,h,wleft,wtop calculated here;

var tools = 'status=yes, scrollbars=yes, resizable=yes';

var win = window.open(url, name,
'width=' + w + ', height=' + h + ', ' +
'left=' + wleft + ', top=' + wtop + ', ' +

win.resizeTo(w, h);
// required where wtop not correctly applied in window.open
win.moveTo(wleft, wtop);

}This works and positions the window where expected.

Sometimes ‘wtop’ is not correctly respected without the inclusion of moveTo(wleft, wtop). For example in IE7 and FF3.6, depending upon window and screen size I am sometimes finding the window vertically aligned at the top of the screen. Safari however seems to position the window correctly without exception.

The problem I have is as follows:

I use the same function and window (by using the same name) to hold further content as requested by the user.

Once the window is open it is reasonable to expect that the user might reposition it to meet their own preferences. Each time the function runs the window gets repositioned back to the calculated position due to moveTo(wleft, wtop).

Is there way a way to amend the script so that moveTo(wleft, wtop) is only run on the first instance of the window launch and not each time it is reused for subsequent content? Or perhaps store the current co-ordinates and use them?

As there may be more than one window open (with different names) I have been trying ways to do this by reference to the window name but without success.