The window object exposes methods for controlling the browser window itself ā opening and closing popups, moving and resizing them, and navigating the page programmatically. This lesson covers window.open(), window.close(), moveTo()/resizeTo(), and the two main ways to redirect a page with window.location: assigning .href and calling .replace().
1Window Manipulation & JavaScript Redirects | Web Dev Tutorial - In-Depth Guide Part 1
Welcome! Today we' You'll learn how to control the browser window itself, from opening new tabs to redirecting users.
// Window & Redirection Control2Window Manipulation & JavaScript Redirects | Web Dev Tutorial - In-Depth Guide Part 2
The ' 'window.open()' method allows you to open a new browser window or tab. You can specify the URL and dimensions.
const myWindow = window.open('https://google.com', '_blank', 'width=500,height=500');3Window Manipulation & JavaScript Redirects | Web Dev Tutorial - In-Depth Guide Part 3
Once you have a reference to a window, you can close it programmatically using ' '.close()'.
myWindow.close(); // Closes the opened window4Window Manipulation & JavaScript Redirects | Web Dev Tutorial - In-Depth Guide Part 4
You can also move and resize windows using ' 'moveTo()' and 'resizeTo()'. Note: Browsers restrict these for security.
myWindow.moveTo(100, 100); // Moves to x=100, y=100
myWindow.resizeTo(800, 600);5Window Manipulation & JavaScript Redirects | Web Dev Tutorial - In-Depth Guide Part 5
Redirection is handled by ' 'window.location'. Assigning a value to '.href' triggers a navigation.
window.location.href = 'https://github.com';
// Or use replace to remove the current page from history
window.location.replace('https://github.com');6Window Manipulation & JavaScript Redirects | Web Dev Tutorial - In-Depth Guide Part 6
Window manipulation and redirects mastered! You now have full control over the user
<h1>Control: Absolute</h1>7Step-by-Step Breakdown
Welcome! Today we' You'll learn how to control the browser window itself, from opening new tabs to redirecting users.
The ' 'window.open()' method allows you to open a new browser window or tab. You can specify the URL and dimensions.
Once you have a reference to a window, you can close it programmatically using ' '.close()'.
Checkpoint: Which window method is used to close a window that was previously opened with JavaScript?
- āexit()
- āclose()
- āremove()
You can also move and resize windows using ' 'moveTo()' and 'resizeTo()'. Note: Browsers restrict these for security.
Redirection is handled by ' 'window.location'. Assigning a value to '.href' triggers a navigation.
Checkpoint: What is the difference between window.location.href and window.location.replace()?
- āThey are exactly the same
- āreplace() removes the current page from the session history
Window manipulation and redirects mastered! You now have full control over the user
Level Up š
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Warn Users Before Opening Unexpected New Windows
A link or button that silently triggers window.open() can disorient screen reader and keyboard users who don't expect a new browsing context to appear; indicate in the accessible name (e.g. 'Opens in a new window') and manage focus so it lands somewhere sensible in the new window rather than leaving the user lost.
SEO Implications
- 1
Client-Side JavaScript Redirects Are Slower and Less SEO-Friendly Than Server-Side Redirects
Setting window.location.href in JavaScript only happens after the page has loaded and executed scripts, meaning crawlers may briefly index the original page before the redirect fires, and real users experience a visible flash of the wrong page. A server-side 301/302 redirect (via HTTP headers) is faster and more reliably followed by search engines.
Best Practices
Use location.replace() for Post-Action Redirects Like Login or Form Submission
If a redirect happens automatically after a successful action (like logging in), use replace() so the previous page isn't kept in history ā otherwise clicking 'back' would resubmit the user into a stale pre-login page or, worse, resubmit a form.
Never Rely on window.moveTo()/resizeTo() Working, Because Browsers Restrict Them by Default
Most modern browsers silently ignore moveTo()/resizeTo() calls on windows the current script didn't open, and some restrict them even on script-opened windows. Don't build critical UI logic around these methods succeeding ā treat window sizing as advisory at best.
Frequent Bugs
window.open() is blocked by the browser's popup blocker even though the code looks correct.
Popup blockers only allow window.open() to succeed when it's called synchronously in direct response to a user gesture (like a click handler). Calling it inside an async callback, a setTimeout, or after an awaited fetch usually gets silently blocked ā trigger it directly inside the synchronous click handler instead.
Using window.location.href for a post-login redirect lets users hit 'back' and land on a broken authenticated-only page.
href-based redirects add a new history entry, so the browser's back button returns to the pre-redirect page, which may now be invalid. Use window.location.replace() instead, which swaps the current history entry rather than adding a new one.
Real-World Examples
Redirecting After a Successful Login Without Polluting History
An app needed to send a user to their dashboard immediately after a successful login API call, without leaving the login page in browser history (so clicking back wouldn't return them to a stale login form).
async function handleLogin(credentials) {
const res = await fetch('/api/login', { method: 'POST', body: JSON.stringify(credentials) });
if (res.ok) {
window.location.replace('/dashboard');
}
}