JavaScript EditorFree JavaScript Editor     DHTML Editor 

Main Page Previous Section Next Section

6.2 Positioning the Main Window

NN 4, IE 4

6.2.1 Problem

You want to move the top-left corner of the browser window to a specific point on the screen.

6.2.2 Solution

Version 4 and later browsers provide two window object methods that adjust the position of the browser window: moveTo( ) and moveBy( ). To move the window to a screen coordinate point, use the moveTo( ) method:

window.moveTo(10, 20);

To shift the position of the window by a known pixel amount, use the moveBy( ) method:

window.moveBy(0, 10);

The window remains the same size when you move it.

6.2.3 Discussion

The coordinate space of the screen is laid out such that the top-left corner of the video display area is point 0,0. The viewable area of your screen has positive coordinate values for both numbers. Negative values are "off the screen," as are values that are larger than the number of pixels displayed on the screen.

While Internet Explorer moves the window completely off screen if the parameters dictate it, Netscape Navigator resists doing so. In fact, the browser resists moving any portion of the window out of view if at all possible. Moving the browser window completely out of view is an unfriendly thing to do, especially in Windows, where the window will reveal its existence in the Taskbar, but the user won't be able to see its contents. The user must then use the Taskbar's context menu to close the window and application. Hidden windows such as this have, in the past, been used to exploit security flaws in Internet Explorer to carry out such nefarious tasks as monitoring activity in another window.

While you can set the position of a window created by script (see Recipe 6.4), you can also modify the position after the window has appeared. As long as the script that creates the new window maintains a reference to the subwindow in a global variable, you can reference that window's moveTo( ) and moveBy( ) methods.

6.2.4 See Also

Recipe 6.4 for resizing a script-generated window.

    Main Page Previous Section Next Section

    JavaScript EditorJavaScript Checker     DHTML Editor