faqts : Computers : Programming : Languages : JavaScript : Windows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 21 people (86%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I move a window around the screen?

Aug 11th, 2002 05:33
Martin Honnen,


Here is some example code that allows you to open a popup of a 
spefified size which is then moved counter clock wise around the 
screen once. It has been tested to work with NN4 (one problem on WinXP 
is that window.screen.availHeight is not correctly given), with NN6 
and with IE6. It should work with IE4+.
<html>
<head>
<title>
moving popup
</title>
<script type="text/javascript">
function MovingPopup (url, width, height) {
  this.id = MovingPopup.popups.length;
  MovingPopup.popups[this.id] = this;
  this.width = width;
  this.height = height;
  this.window = window.open(url, 'p' + this.id,
    'width=' + width + ',height=' + height + 
    'scrollbars,resizable' +
    ',top=0,left=0'
  );
  this.window.resizeTo(this.width, this.height);
  this.window.focus();
  if (this.init()) {
    this.start('moveDown');
  }
}
MovingPopup.prototype.init = function () {
  if (window.screen) {
    this.x = 0;
    this.y = 0;
    this.dx = this.dy = 10;
    this.speed = 40;
    this.availLeft = 0;
    this.availTop = 0;
    this.availWidth = screen.availWidth;
    this.availHeight = screen.availHeight;
    this.minLeft = 0;
    this.maxLeft = this.availWidth - this.width;
    this.minTop = 0;
    this.maxTop = this.availHeight - this.height;
    return true; 
  }
  else
    return false;
}
MovingPopup.prototype.start = function (methodName) {
  if (this.tid)
    clearInterval(this.tid);
  if (methodName) 
    this.methodName = methodName;
  if (this.methodName) {
    this.tid = setInterval(
      'MovingPopup.popups[' + this.id + '].' + this.methodName 
+ '();', this.speed);
  }
}
MovingPopup.prototype.moveDown = function () {
  this.y += this.dy;
  if (this.y <= this.maxTop)
    this.window.moveTo(this.x, this.y);
  else
    this.start('moveRight');
}
MovingPopup.prototype.moveRight = function () {
  this.x += this.dx;
  if (this.x <= this.maxLeft)
    this.window.moveTo(this.x, this.y);
  else
    this.start('moveUp'); 
}
MovingPopup.prototype.moveUp = function () {
  this.y -= this.dy;
  if (this.y >= 0)
    this.window.moveTo(this.x, this.y);
  else
    this.start('moveLeft');
}
MovingPopup.prototype.moveLeft = function () {
  this.x -= this.dx;
  if (this.x >= 0)
    this.window.moveTo(this.x, this.y);
}
MovingPopup.prototype.stop = function () {
  if (this.tid)
    clearInterval(this.tid);
}
MovingPopup.popups = new Array();
</script>
<script type="text/javascript">
var movingPopup;
</script>
</head>
<body>
<form name="gui">
url:
<input type="text" name="url" size="60" 
       value="jsInterpreter.html"
/>
<br />
<input type="button" value="start"
       onclick="if (!movingPopup)
                  movingPopup = new MovingPopup(this.form.url.value, 
200, 200);
                else
                  movingPopup.start();"
/>
<input type="button" value="stop"
       onclick="if (movingPopup)
                  movingPopup.stop();"
/>
</form>
</body>
</html>