The moveBy() method shifts a browser window by a relative pixel offset. This tutorial covers syntax, positive and negative values, how it differs from moveTo(), popup usage, browser restrictions, five examples, and when not to use it.
01
Syntax
moveBy(x, y)
02
Relative
From current spot
03
vs moveTo
Absolute coords
04
Popups
popup.moveBy()
05
Signs
+/− for direction
06
Limits
Often blocked
Fundamentals
Introduction
Every browser window has a position on the screen. The window.moveBy() method nudges that window by a number of pixels—without you needing to know the current coordinates. Pass 100 for x to shift right; pass -50 for y to shift up.
Like close() and focus(), movement APIs are limited on tabs the user opened themselves. Browsers prevent sites from dragging the user’s main window around unexpectedly. Test with popups created via window.open(), especially when the user clicks a button to trigger the move.
Concept
Understanding the moveBy() Method
window.moveBy(deltaX, deltaY) adds deltaX to the window’s current horizontal position and deltaY to its vertical position. Both arguments are numbers (pixels). The method returns undefined.
Called on a popup reference, it moves that child window: popupWindow.moveBy(20, 10). This is the pattern you will see in helper-window tutorials and legacy desktop-style web tools.
💡
Beginner Tip
Think of moveBy as “walk from here” and moveTo as “teleport to coordinates.” For layout inside a page, move DOM elements with CSS—not the browser window.
Foundation
📝 Syntax
General form of Window.moveBy():
JavaScript
window.moveBy(deltaX, deltaY);
// move a popup reference:
popupWindow.moveBy(20, 10);
Each click adds another relative offset. Small steps feel less jarring than huge jumps if movement is allowed.
Example 3 — Opener Moves a Popup
Store the open() reference and call moveBy on the child window.
JavaScript
let popupWindow = null;
document.getElementById("openBtn").onclick = function () {
popupWindow = window.open("", "MoveDemo", "width=320,height=200,left=200,top=120");
popupWindow.document.write("<p>I am a movable popup</p>");
};
document.getElementById("movePopupBtn").onclick = function () {
if (popupWindow && !popupWindow.closed) {
popupWindow.moveBy(30, 20);
console.log("Popup moved by (30, 20).");
}
};
Open popup → click Up/Down/Left/Right → popup shifts 15px per click.
How It Works
Small repeated moveBy calls build a simple repositioning UI. Store deltas in data-* attributes to keep HTML readable for beginners.
Applications
🚀 Common Use Cases
Popup tool windows — nudge helper panels the user opened from your app.
Legacy desktop-style UIs — multi-window tutorials and intranet tools.
Step positioning — fine-tune popup placement after open() when moveTo is overkill.
Direction controls — arrow buttons that shift a child window incrementally.
Learning demos — contrast relative moveBy with absolute moveTo.
Not for responsive layout — use CSS media queries instead of moving the browser window by screen width.
🧠 What Happens When You Call moveBy()
1
Pass deltas
Script supplies horizontal and vertical pixel offsets.
Args
2
Policy check
Browser verifies the window may be repositioned.
Security
3
Add to position
Current screenX/screenY plus deltas becomes the new location.
Relative
4
→
Window shifts (maybe)
If allowed, the window moves on screen; method returns undefined.
Important
📝 Notes
Do not auto-move windows on page load—it frustrates users and is usually blocked.
Moving off-screen can make a popup hard to find; clamp or reset with moveTo when needed.
Fullscreen and maximized windows may ignore or clamp movement.
Mobile browsers often lack traditional movable windows; popups may open as tabs.
moveBy affects the outer window—not elements inside the page (use CSS for those).
Pair with window.open() features string left and top for initial placement.
Compatibility
Browser Support
Window.moveBy() exists in all major browsers, but whether movement succeeds depends on window type and browser policy—not on missing API support.
✓ Universal API · Restricted behavior
Window.moveBy()
The method is available in Chrome, Firefox, Safari, Edge, and Opera. Script-opened popups move most reliably; user-opened main tabs are often protected.
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
moveBy()Universal
Bottom line: Test with popups from window.open() during user clicks. Never rely on moving the user's primary tab.
Wrap Up
Conclusion
window.moveBy() shifts a browser window by relative pixel offsets. It complements moveTo() and fits popup workflows more often than everyday page layout.
Use small steps, trigger moves from user actions, and prefer CSS for in-page design. For helper windows, open with window.open() and call popup.moveBy() on the returned reference.
Use moveBy on popups you opened with window.open()
Trigger moves from button clicks when possible
Keep offsets small for predictable nudging
Use negative values for left/up movement
Learn moveTo() when you need absolute coordinates
❌ Don’t
Move the user’s main tab without clear consent
Use moveBy for responsive layout by screen width
Apply huge offsets that push windows off-screen
Confuse window movement with DOM/CSS positioning
Auto-move windows on every page load
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about moveBy()
Your foundation for relative window movement in JavaScript.
5
Core concepts
📝01
Syntax
moveBy(dx, dy)
API
→02
Relative
From current spot.
Delta
🚫03
Restricted
Main tab blocked.
Policy
🔌04
Popups
popup.moveBy
open()
📍05
vs moveTo
Absolute coords.
Pair
❓ Frequently Asked Questions
It moves the current window by a relative offset. The first argument is horizontal pixels (positive = right, negative = left); the second is vertical pixels (positive = down, negative = up).
moveBy(x, y) shifts from the current position. moveTo(x, y) jumps to absolute screen coordinates. Use moveBy for nudging; use moveTo when you know the exact top-left position.
No. Modern browsers restrict programmatic window movement for security and UX. moveBy() may be ignored on tabs the user opened directly. It works more reliably on popups your script opened with window.open().
No. It returns undefined. There is no built-in way to read the new coordinates from the return value—use screenX/screenY on the window if needed.
Yes. Keep the reference from window.open() and call popup.moveBy(dx, dy) on that object, often after a user clicks a button on the opener.
No. Responsive design belongs in CSS and DOM layout. moveBy() is for rare popup positioning workflows—not for adjusting layout based on screen width.
Did you know?
Early web apps used moveBy and moveTo together with resizeBy to mimic floating palettes. Modern sites achieve similar UX with in-page panels and CSS—without fighting browser window restrictions.