JavaScript Window moveTo() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.moveTo() moves the current window to an absolute screen position (x, y) (MDN). Use moveBy() for a relative shift.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.moveTo. Start with this line.

JavaScript
moveTo(x, y)
CallWhat it does
moveTo(x, y)Primary signature.
window.moveTo(...)Call with the arguments your case needs.

Built into the browser. Call window.moveTo(...) (or the global moveTo(...)) — no import required.

Library declarations
JavaScript
moveTo(x, y)

Parameters

ParameterDescription
xRequired. Horizontal screen coordinate
yRequired. Vertical screen coordinate

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Top-Left (0, 0)

MDN sample: move the window to the origin of the screen.

JavaScript
function origin() {
  window.moveTo(0, 0);
}

origin();
Try It Yourself

How It Works

  1. Call moveTo(0, 0). Absolute target is the screen corner.
  2. Browser may ignore it on the main tab.
  3. Return is undefined. No success flag.
  4. Try It lab uses a popup for a better chance of success.

Example 2 — Absolute Spot

Place a popup at fixed screen coordinates.

JavaScript
const popup = window.open(
  "about:blank",
  "spot",
  "width=320,height=200"
);
if (popup) {
  popup.moveTo(120, 80);
}
Try It Yourself

How It Works

  1. Open a popup with size features.
  2. Call moveTo(120, 80) on that window.
  3. Absolute, not relative — same target every time.
  4. Allow popups in the browser for the lab to work.

Example 3 — Center Popup

Compute coordinates from screen size and window outer size.

JavaScript
const w = 400;
const h = 300;
const popup = window.open(
  "about:blank",
  "center",
  "width=" + w + ",height=" + h
);
if (popup) {
  const x = Math.round((screen.availWidth - w) / 2);
  const y = Math.round((screen.availHeight - h) / 2);
  popup.moveTo(x, y);
}
Try It Yourself

How It Works

  1. Choose width/height. Match the open features.
  2. Compute center. (avail − size) / 2.
  3. Call moveTo(x, y). Absolute placement.
  4. Alternative: pass left/top in open() features.

Example 4 — vs moveBy

Same idea, absolute vs relative APIs.

JavaScript
// Absolute: always go to (200, 100)
window.moveTo(200, 100);

// Relative: shift 40px right and 20px down from current
window.moveBy(40, 20);
Try It Yourself

How It Works

  1. moveTo — destination is fixed on the screen.
  2. moveBy — delta from the current position.
  3. Pick by intent. Place vs nudge.
  4. Same restrictions apply to both in modern browsers.

Example 5 — open then moveTo

User-gesture pattern: open a blank window, then move it.

JavaScript
document.getElementById("btn").onclick = function () {
  const win = window.open(
    "about:blank",
    "moved",
    "width=300,height=200"
  );
  if (!win) {
    document.getElementById("out").textContent = "Popup blocked";
    return;
  }
  win.document.write("

Moved with moveTo

"); win.moveTo(50, 50); document.getElementById("out").textContent = "Called moveTo(50, 50)"; };
Try It Yourself

How It Works

  1. Click first. User gesture helps open succeed.
  2. Open a named popup with size features.
  3. Call moveTo on the popup — not always on window.
  4. Handle blockers. open can return null.

Browser Support

Window.moveTo() is marked Baseline Widely available on MDN (since July 2015). Support for actually moving a window varies by browser policy and OS.

✓ Baseline · Widely available*

moveTo()

API is widely available; successful moves depend on popup / OS rules.

API OK Core tag support
Chrome Yes*
Restricted
Edge Yes*
Restricted
Firefox Yes*
open() only / single tab
Safari Yes*
Restricted
Opera Yes*
Restricted
HTML element 100% supported

Bottom line: Teach the API, but design for popups — and accept that many environments will ignore the call.

Frequently Asked Questions

It moves the current window so its top-left corner is at absolute screen coordinates (x, y), in CSS pixels from the top-left of the screen.
No. MDN marks Window.moveTo() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard — but browsers restrict when it actually moves a window.
moveTo(x, y) sets an absolute position. moveBy(dx, dy) shifts relative to the current position.
Browsers often block moving the main tab. Firefox (since 7) also blocks windows not created by window.open() and windows with more than one tab. Some environments (Wayland, mobile) may not move windows at all.
undefined. Check screenX / screenY afterward if you need the position (when the browser allows the move).
After opening with window.open(), call moveTo((screen.availWidth - outerWidth) / 2, (screen.availHeight - outerHeight) / 2) — or set left/top in the open() features string.

Next: open()

Open a new browsing context / popup window.

open() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful