2

I'd like to resize & move a browser window using JavaScript. resizeTo() and moveTo() seem to be my friends here:

window.resizeTo(x,y);
window.moveTo(x,y);

This works, but visually, it's a bit clunky. First, the window is moved to the desired location and then the window gets repainted on the display. Finally, the window is resized to the desired dimensions and gets repainted on the display once more. This all happens within a couple hundred milliseconds but the two discrete step are noticeable and it looks awkward.

What I really want is for these two methods to be atomic, such that they both return before the browser window (UI and all,) gets repainted on the display. Can this more cohesive presentation of window repositioning and resizing be achieved using JavaScript?

ntwk
  • 113
  • 5
  • `moveTo` is not part of any formal [JavaScript/browser specification](https://developer.mozilla.org/en-US/docs/Web/API/window.moveTo), so is probably not advisable. Can the effect you want be accomplished any other way? – Jivings Sep 03 '13 at 19:18
  • Thanks, I didn't know that moveTo was non-standard. That's ok, as I'm just mapping this code to a key in Firefox so that I can control the position &size of the focused window. Another solution would be to wrap [wmctrl](http://tomas.styblo.name/wmctrl/) in some shell script and map that to a key via my window manager. Unfortunately, In that case the key binding is global so some extra logic would be needed to figure out when to do the right thing. Also, other applications could potentially use very same key binding, in which case the window manager's binding would take precedence. – ntwk Sep 03 '13 at 20:51

1 Answers1

1

Use the setTimeout trick to allow the UI to "catch-up".

window.setTimeout(function() {window.resizeTo(x,y)},0);
window.setTimeout(function() {window.moveTo(x,y)},0);
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • I never knew about this hack. [A previous discussion](http://stackoverflow.com/questions/779379/why-is-settimeoutfn-0-sometimes-useful) helped shed some light on the intended effect of calling setTimeout with 0 as the second argument. Unfortunately, in this particular case, it doesn't seem to make my problem go away. I also tried wrapping both window methods inside a single call to setTimeout, but still no joy. – ntwk Sep 03 '13 at 20:24
  • 1
    Moving and resizing windows pretty well went out of style last decade, with modal dialogs taking over that functionality. A lot of work has been spent improving DOM responsiveness, actual window performance seems to have been left behind. – Diodeus - James MacFarlane Sep 03 '13 at 20:28