Javascript Reference - Window moveTo() Method








The moveTo() method moves a window's left and top edge to the specified coordinates.

Browser Support

The moveTo() method is supported in all major browsers.

moveTo Yes Yes Yes Yes Yes

Syntax

window.moveTo(x, y)

Parameter Values

Parameter Description
x Required. A positive or negative number of pixels to move in the horizontal
y Required. A positive or negative number of pixels to move in the vertical




Return Value

No return value.

Example

The following code shows how to Open a new window, and move the new window to the top left corner of the screen.


<!DOCTYPE html>
<html>
<body>
<button onclick="openWin()">Open "myWindow"</button>
<button onclick="moveWin()">Move "myWindow"</button>
<!--   w ww  .  j av  a 2s  .c om-->
<script>
var myWindow;

function openWin() {
    myWindow=window.open("", "myWindow", "width=200, height=100");
    myWindow.document.write("<p>This is 'myWindow'</p>");
}

function moveWin() {
    myWindow.moveTo(500, 100);
    myWindow.focus();
}
</script>

</body>
</html>

The code above is rendered as follows: