JavaScript Window moveTo() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Window position

What You’ll Learn

The moveTo() method places a browser window at exact screen coordinates. This tutorial covers syntax, centering popups, how it differs from moveBy() and open() placement, browser restrictions, five examples, and best practices.

01

Syntax

moveTo(x, y)

02

Absolute

Screen coordinates

03

vs moveBy

Relative nudge

04

Center

Popup on screen

05

Read

screenX, screenY

06

Limits

Often blocked

Introduction

When you open a helper window with window.open(), you may want it at a specific spot—not drifting off-screen. The window.moveTo(x, y) method sets the window’s top-left corner to absolute pixel coordinates on the user’s display.

Coordinates start at the top-left of the screen: x increases to the right, y increases downward. Like moveBy() and close(), movement is often blocked on the user’s main tab but works more reliably on script-opened popups triggered by a click.

Understanding the moveTo() Method

window.moveTo(left, top) takes two numbers and returns undefined. The browser attempts to place the window so its outer top-left corner matches those coordinates. On a popup reference: popupWindow.moveTo(200, 100).

To verify placement (when permitted), read window.screenX and window.screenY—they reflect the window position on screen. Use window.outerWidth and outerHeight when calculating centered positions.

💡
Beginner Tip

Centering is moveTo((screen.width - outerWidth) / 2, (screen.height - outerHeight) / 2)—not screen.width / 2 alone, which puts the window corner at the middle of the screen instead of centering the window.

📝 Syntax

General form of Window.moveTo():

JavaScript
window.moveTo(left, top);
// reposition a popup reference:
popupWindow.moveTo(200, 100);

Parameters

  • left — horizontal pixel coordinate for the window’s left edge.
  • top — vertical pixel coordinate for the window’s top edge.

Return value

  • undefined.

Related APIs

  • window.moveBy(dx, dy) — shift relative to current position.
  • window.open(..., "left=200,top=100") — initial placement at open time.
  • window.screenX / screenY — read current position.
  • window.screen.width / height — screen dimensions for centering math.

⚡ Quick Reference

TopicDetail
Absolute movewindow.moveTo(x, y)
Relative movewindow.moveBy(dx, dy)
Center popup(screenW - outerW) / 2, same for height
At open timeopen(url, name, "left=…,top=…")
Read positionscreenX, screenY
Main user tabOften cannot move

📋 moveTo() vs moveBy() vs open()

Pick absolute placement, relative nudging, or initial coordinates when the popup opens.

moveTo
moveTo(200, 100)

Jump to coordinates

moveBy
moveBy(20, 10)

Nudge from here

open()
left=200,top=100

Set at creation

In-page
CSS position

Move elements, not window

Examples Gallery

Use the Try-it links to place windows from button clicks. Popups from window.open() are the most reliable way to see moveTo() in action.

📚 Getting Started

Jump the window to fixed screen coordinates.

Example 1 — Move to Fixed Coordinates

Place the top-left corner at (100, 200).

JavaScript
window.moveTo(100, 200);
console.log("moveTo(100, 200) called — may be blocked on main tabs.");
Try It Yourself

How It Works

The method requests an absolute position. Whether it succeeds depends on browser policy and whether the window is a user tab or a script popup.

📈 Practical Patterns

Centering, opener control, open-time placement, and reading coordinates.

Example 2 — Center a Popup on the Screen

Subtract the window size so the popup is visually centered—not just its top-left corner.

JavaScript
function centerWindow(win) {
  const left = (window.screen.width - win.outerWidth) / 2;
  const top = (window.screen.height - win.outerHeight) / 2;
  win.moveTo(left, top);
}

const popup = window.open("", "Centered", "width=400,height=300");
if (popup) {
  centerWindow(popup);
  console.log("Popup centered.");
}
Try It Yourself

How It Works

outerWidth and outerHeight include window chrome (title bar, borders). The formula places equal space on left/right and top/bottom.

Example 3 — Opener Repositions a Popup

Open a popup, then call moveTo on its reference from the parent page.

JavaScript
let popupWindow = null;

document.getElementById("openBtn").onclick = function () {
  popupWindow = window.open("", "MoveToDemo", "width=320,height=200");
  popupWindow.document.write("<p>Movable popup</p>");
};

document.getElementById("placeBtn").onclick = function () {
  if (popupWindow && !popupWindow.closed) {
    popupWindow.moveTo(300, 150);
    console.log("Popup moved to (300, 150).");
  }
};
Try It Yourself

How It Works

The opener keeps a live reference. moveTo on that object repositions only the child window—the parent tab stays put.

Example 4 — Initial Position with open() Features

Set left and top when creating the popup; use moveTo later if you need to adjust.

JavaScript
const popup = window.open(
  "",
  "Placed",
  "width=360,height=240,left=200,top=120"
);

// Fine-tune after open (optional)
if (popup) {
  popup.moveTo(220, 140);
  console.log("Popup opened and fine-tuned.");
}
Try It Yourself

How It Works

Many tutorials set position once in the open() third argument. moveTo() is the follow-up API when you need to reposition an existing window.

Example 5 — Read Position with screenX and screenY

After attempting a move, log coordinates from the popup (when readable).

JavaScript
const popup = window.open("", "Coords", "width=280,height=160,left=100,top=80");

if (popup) {
  popup.moveTo(250, 180);
  console.log("screenX:", popup.screenX, "screenY:", popup.screenY);
}
Try It Yourself

How It Works

Cross-origin restrictions may block reading some popup properties, but same-origin blank popups usually expose screenX and screenY for debugging placement.

🚀 Common Use Cases

  • Centered dialogs — place OAuth or login popups in the middle of the screen.
  • Fixed tool palettes — park helper windows at known coordinates in legacy apps.
  • Multi-monitor layouts — calculate offsets for secondary displays (with care and user consent).
  • After open() — fine-tune position when the features string is not enough.
  • Pair with moveBy() — teleport roughly with moveTo, nudge with moveBy.
  • Not for page layout — position DOM elements with CSS, not the browser window.

🧠 What Happens When You Call moveTo()

1

Pass coordinates

Script supplies absolute left and top pixel values.

Args
2

Policy check

Browser verifies the window may be repositioned.

Security
3

Window relocates

Top-left corner moves to (left, top) if allowed.

Place
4

Verify if needed

Read screenX / screenY; method returns undefined.

📝 Notes

  • Keep coordinates on-screen—negative or huge values may clamp or fail silently.
  • Do not auto-move windows on page load without user action.
  • Maximized and fullscreen windows ignore or override moveTo.
  • Mobile environments may not expose movable windows at all.
  • Prefer open() left/top for first placement; moveTo for updates.
  • Centering requires subtracting outerWidth and outerHeight, not just halving screen size.

Browser Support

Window.moveTo() exists in all major browsers, but whether repositioning succeeds depends on how the window was opened and browser security policy.

Universal API · Restricted behavior

Window.moveTo()

Available in Chrome, Firefox, Safari, Edge, and Opera. Script-opened popups reposition most reliably during user-initiated flows.

100% API availability
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
moveTo() Universal

Bottom line: Test with popups from window.open(). Do not depend on moving the user's primary tab.

Conclusion

window.moveTo() places a browser window at absolute screen coordinates. It pairs with moveBy() for relative nudges and with open() for initial popup placement.

Center popups with the correct width/height math, trigger moves from user clicks, and use CSS for in-page layout—not window APIs.

💡 Best Practices

✅ Do

  • Use moveTo on popups opened with window.open()
  • Center with (screen - outer) / 2 formula
  • Set left and top in open() when possible
  • Trigger placement from button clicks
  • Read screenX / screenY to debug placement

❌ Don’t

  • Use screen.width / 2 alone for centering
  • Move the user’s main tab without consent
  • Place windows off-screen on purpose
  • Confuse window coords with DOM getBoundingClientRect
  • Rely on moveTo for responsive page layout

Key Takeaways

Knowledge Unlocked

Five things to remember about moveTo()

Your foundation for absolute window placement in JavaScript.

5
Core concepts
📍 02

Absolute

Screen coords.

Place
🚫 03

Restricted

Main tab blocked.

Policy
🔌 04

Center

Subtract outer size.

Math
🔄 05

vs moveBy

Relative nudge.

Pair

❓ Frequently Asked Questions

It moves the window so its top-left corner sits at absolute screen coordinates (x, y) measured in pixels from the top-left of the screen.
moveTo(x, y) sets an exact position. moveBy(dx, dy) shifts relative to the current spot. Use moveTo to center or place a popup; use moveBy for small nudges.
No. Browsers restrict programmatic window movement on tabs the user opened directly. moveTo() works more reliably on popups created with window.open(), especially after a user click.
Use: moveTo((screen.width - outerWidth) / 2, (screen.height - outerHeight) / 2). Subtract the window's outer width and height so the center of the window aligns with the screen center.
No. It returns undefined. Read window.screenX and window.screenY afterward if you need to verify position (when the browser allows).
Yes. window.open() accepts left and top in the features string, e.g. open(url, name, 'width=400,height=300,left=200,top=100'). moveTo() adjusts position after the window exists.
Did you know?

The window.open() features string accepts left and top in the same coordinate system as moveTo()—many apps set position once at open time and only call moveTo() when the user asks to recenter a popup.

Continue to open()

Learn how to open new browser windows and popups with the window.open() method.

open() tutorial →

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.

6 people found this page helpful