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
Fundamentals
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.
Concept
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.
Foundation
📝 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.
Cheat Sheet
⚡ Quick Reference
Topic
Detail
Absolute move
window.moveTo(x, y)
Relative move
window.moveBy(dx, dy)
Center popup
(screenW - outerW) / 2, same for height
At open time
open(url, name, "left=…,top=…")
Read position
screenX, screenY
Main user tab
Often cannot move
Compare
📋 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
Hands-On
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.");
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.");
}
Cross-origin restrictions may block reading some popup properties, but same-origin blank popups usually expose screenX and screenY for debugging placement.
Applications
🚀 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.
Important
📝 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
moveTo()Universal
Bottom line: Test with popups from window.open(). Do not depend on moving the user's primary tab.
Wrap Up
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.
Confuse window coords with DOM getBoundingClientRect
Rely on moveTo for responsive page layout
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about moveTo()
Your foundation for absolute window placement in JavaScript.
5
Core concepts
📝01
Syntax
moveTo(x, y)
API
📍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.