I am new to JavaScript and just started learning window methods.
I wanted to write this method to get a new window to move from the left end to the right end of the screen. However, the code doesn’t work.
Could someone please tell me where I’ve gone wrong.
<!doctype html>
<html>
<title>Avoid Window</title>
<head>
<script type="text/javascript">
var MovingWindow;
function CreateWindow()
{
MovingWindow = window.open('','MovingWindow','width=100,height=100,menubar=0,toolbar=0,location=0,resizeable=0,status=0');
window.MovingWindow.moveTo(0,0);
while( window.MovingWindow.screenX < window.screen.width - window.MovingWindow.width)
{
window.MovingWindow.moveBy(1,0);
}
}
</script>
</head>
<body>
<h1>Avoid Window</h1>
<p> Click on the button below to create a window which moves from one end of the screen to the other end of the screen</p>
<input type="Button" onClick="CreateWindow();" value="Create Window"/>
<input type="Button" onClick="MoveWindow();" value="Move Window"/>
</body>
</html>
You can’t do animations in a loop. As long as the function is running, the user interface is not updated. Your function will change the coordinates of the window, and when the function ends the window will be redrawn at the final position.
Use the
window.setIntervalmethod to start an interval where you can move the window:However, as Daniel A. pointed out, some browsers doesn’t allow client script to move windows. If it doesn’t work in your browser, you can change the settings just to test that the
setIntervalapproach works.