DEV Community

Joshua Amaju
Joshua Amaju

Posted on

2 1

Self Closing Browser Tab with JavaScript

Did You Know You Can Close a Browser Tab Using JavaScript?

It might surprise you to learn that you can close a browser tab programmatically with JavaScript. Yes, it’s possible—using the window.close() method.

This method is typically used only with popup windows, so I was just as surprised as you might be when I first discovered that you can also use it on regular tabs when a page I visited unexpectedly closed after I submitted a form. It felt strange—almost like something you'd expect from a mobile app crashing due to a bug. But on the web, things don’t work that way.

Unlike mobile applications, web pages or browser tabs don’t simply "crash" and close due to a fatal error. At worst, the page may freeze, or you’ll encounter an error message. So, how was this tab closing itself? Intrigued, I decided to dig deeper into the mechanics behind this behaviour.

If you’re curious to see this in action, you can try it right now using your browser's developer tools. Here’s how:

  • Open the Developer Tools on this page (usually by pressing F12 or Ctrl+Shift+I on Windows, or Cmd+Option+I on macOS).
  • Navigate to the Console tab.
  • Type the following command and press Enter:
window.close();
Enter fullscreen mode Exit fullscreen mode

Or

Live demo

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (2)

Collapse
 
kurealnum profile image
Oscar

I encountered this on an online textbook I was using a few weeks ago. It's a neat feature, but I feel like it's not all that great in terms of accessibility (at least in the textbook that I was using). Awesome for popup windows though.

Collapse
 
joshuaamaju profile image
Joshua Amaju

I agree that it's not great for accessibility. It's not something web pages normally do, which leaves the user confused about what happened.

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay