JavaScript Window Close: W3Schools Tutorial

Sarah Jul 13, 2026

While JavaScript can perform a myriad of tasks on webpages, sometimes we just need a window to close. Here, we'll explore JavaScript's window.close method, focusing on its functionalities and usage, guided by the renowned W3Schools tutorials and documentation.

Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial

Whether you're new to JavaScript or looking to refine your skills, understanding window.close is crucial. It's not just about closing a window, but also about managing user experience and enhancing web application functionality.

Javascript Window.print()
Javascript Window.print()

Understanding window.close in JavaScript

window.close is a method in JavaScript's Window interface. It's used to close the current window or a specific window opened with the window.open method.

Back to Basics - JavaScript Closures
Back to Basics - JavaScript Closures

The syntax is simple: window.close() without any arguments. The window to be closed is determined by the calling context.

Closing the Current Window

Toggle Fullscreen in Javascript
Toggle Fullscreen in Javascript

The window.close method, when called without an argument, closes the current page's window. This is often used in form of a close link or button in the footer or header of a web page.

Here's a simple example from W3Schools:

<a href="javascript:void(0)" onclick="window.close();">Close Window</a>

Closing a Specific Window

Create a RAIN EFFECT with HTML CSS and JavaScript NOW!
Create a RAIN EFFECT with HTML CSS and JavaScript NOW!

You can also close a window opened with window.open. The window object returned by window.open^{\*}}\ fรถpen is used to target the window to be closed.

Here's an example from W3Schools that opens a new window and closes it after 2 seconds:

var win = window.open("", "_blank", "width=200,height=100,resizeable=no,scrollbars=no");
setTimeout(function(){ win.close(); }, 2000);

Safety Measures and Limitations

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

While window.close is useful, it's not always effective. Browsers often restrict its use for security reasons. Generally, a window can only be closed by scripts originating from the same source (same protocol, port, and domain).

Security Restrictions

an image of some type of webpage with dark colors and blue text on it
an image of some type of webpage with dark colors and blue text on it
Fun JavaScript Coding Project Ideas for Beginners: Practice Front End Web Development (in 2020)
Fun JavaScript Coding Project Ideas for Beginners: Practice Front End Web Development (in 2020)
the web programming poster is shown
the web programming poster is shown
Create Image Accordion Slider With HTML CSS And JavaScript
Create Image Accordion Slider With HTML CSS And JavaScript
Javascript String Methods
Javascript String Methods
an image of programming tricks and tips for beginners to learn in this video game
an image of programming tricks and tips for beginners to learn in this video game
JavaScript study guide for fast learning
JavaScript study guide for fast learning
Closeup of Java Script, CSS and HTML Code. Stock Image - Image of hosting, document: 116882015
Closeup of Java Script, CSS and HTML Code. Stock Image - Image of hosting, document: 116882015
Synchronous & Asynchronous Javascript
Synchronous & Asynchronous Javascript
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
JavaScript Projects That Will Make You Better Fast ๐Ÿ˜ณ
JavaScript Projects That Will Make You Better Fast ๐Ÿ˜ณ
Javascript setTimeout() method
Javascript setTimeout() method
Filter JavaScript Arrays Like a BOSS: Beginner's Guide
Filter JavaScript Arrays Like a BOSS: Beginner's Guide
JavaScript Roadmap + Projects
JavaScript Roadmap + Projects
the web programming poster is shown
the web programming poster is shown
an image of a computer screen with some type of text and pictures on the screen
an image of a computer screen with some type of text and pictures on the screen
DOM & Events
DOM & Events
JavaScript Tips to Change Border Radius & Background Color
JavaScript Tips to Change Border Radius & Background Color
an image of a web page with different types of text
an image of a web page with different types of text
JavaScript Basics | Chapter 1 ๐Ÿ“– | Part 1
JavaScript Basics | Chapter 1 ๐Ÿ“– | Part 1

Due to security restrictions, window.close won't work on some browsers or pages, such as:

  • Pop-up blockers
  • Windows opened by other scripts (for security reasons)
  • Windows opened by user action, like clicking a link

Alternative Method: window.onbeforeunload

If window.close() doesn't work, you can use the page unload event (onbeforeunload) to achieve similar results:

Here's an example:

window.onbeforeunload = function() {
  return 'Are you sure you want to leave this page?';
}

This method displays a confirmation dialog before the user leaves the page, effectively acting as a close mechanism.

Now that you've learned about window.close, you can apply this knowledge to manage windows in your web applications. Always remember, with great power comes great responsibility - use window.close judiciously to enhance user experience, not degrade it. Happy coding!