Start Working With Browser Windows in JavaScript - How to move the current browser window using JavaScript
(Page 2 of 5 )
Now, let us try to develop a simple script (JavaScript) to move the current browser window. Have a look at the following code:
<html>
<head>
<meta name=vs_targetSchema
content="http://schemas.microsoft.com/intellisense/ie5">
<script id="clientEventHandlersJS" language="javascript">
<!--
function Apply(x,y)
{
window.moveTo(x,y);
//window.moveBy(x,y);
}
function ButtonMove_onclick() {
var x = document.all("txtX").value;
var y = document.all("txtY").value;
Apply(x,y);
}
//-->
</script>
</head>
<body>
<form id="form1">
Enter X:<input type="text" id="txtX" NAME="txtX"
value="100"><br>
Enter Y:<input type="text" id="txtY" NAME="txtY"
value="200"><br>
<input type="button" value="Show" id="ButtonMove"
name="ButtonMove" onclick="return ButtonMove_onclick()">
</form>
</body>
</html>
Within the above code, I managed to create two textboxes named “txtX” and “txtY” and a button named “ButtonMove.” When the button (buttonMove) is clicked, the control goes to the function “ButtonMove_onclick.” This function mainly captures the values (X and Y) given by the user and finally calls another function, “Apply” (along with passing those values).
The function “Apply” mainly contains the following statement:
window.moveTo(x,y);
The “window” is a built-in object in JavaScript which contains a method, “moveTo.” The method accepts two parameters, left and top (or x and y). The method automatically moves the window to the specified left and top values provided. The above statement moves the current browser window (on which your web page is displayed) to the left and top values you specified within the textboxes. Let us consider another alternative:
window.moveBy(w,h);
This is very similar to what I explained in the previous section.
Next: How to maximize the current browser window using JavaScript >>
More JavaScript Articles
More By Jagadish Chaterjee