How to Close a Browser Tab/Window with JavaScript

Closing a temporary window after its work is done


How to Close a Browser Tab/Window with JavaScript

by Christopher Heng, thesitewizard.com

I was asked by a visitor how he could close a browser window or tab using JavaScript. This article addresses that question.

Prerequisite

Since this was written in response to a JavaScript question, it assumes that you at least know how to insert JavaScript code into a web page.

How to Close a Window with JavaScript

To close a window or tab that was opened using JavaScript, call window.close(). For example, the following will close the current window/tab.

<script type="text/javascript">
  window.close() ;
</script>

Note that window.close() will only work under the following conditions:

  1. On all modern browsers, the web page to be closed must be the first in that window/tab's session history. That is, if you hit the Back button on the browser, you will not go to a previous page in that window/tab because there is none.

    You can easily accomplish that by opening that page either with JavaScript, for example, by using window.open(), or through a link that has a target="_blank" attribute. You can find example code for the former in the demo below, and the latter in the article on opening links in a new window or tab.

    If you only test your code in an old browser (eg, old versions of Chrome or Firefox, or any version of Internet Explorer), you will end up with the mistaken impression that it works fine even if the above conditions are not true. Newer browser versions impose these restrictions for security (and other) reasons.

  2. Modern browsers will also resist your attempt to trick them into thinking that an existing window/tab was opened with JavaScript when it was not. Some older versions fell for such trickery, but these methods should no longer work in the current versions of Chrome and Firefox.

Demo

Before you click the demo buttons below, please note the following:

Source Code for the Demo

The HTML code for the buttons is as follows:

<button type="button" onclick="tsw_open_demo_window();">Open demo in a new window/tab</button>
<button type="button" onclick="window.close();">Close current window if possible</button>

It is just the standard HTML code for buttons, with the addition of onclick handlers that are invoked when someone clicks them.

The JavaScript for tsw_open_demo_window(), which is called when the "Open demo" button is clicked, is:

function tsw_open_demo_window()
{
  window.open( "close-browser-tab-or-window.shtml#demo" );
}

I used a relative URL here, since I'm just opening this same page, but you can use an absolute one (ie, a complete address, including the "http://" or "https://" portion) if you wish.

Since the "Close current window" button does nothing but call window.close(), I placed the JavaScript directly in its onclick attribute.

Copyright © 2019-2021 Christopher Heng. All rights reserved.
Get more free tips and articles like this, on web design, promotion, revenue and scripting, from https://www.thesitewizard.com/.

thesitewizard™ News Feed (RSS Site Feed)  Subscribe to thesitewizard.com newsfeed

Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.

Related Articles

New Articles

How to Link to This Page

It will appear on your page as:

How to Close a Browser Tab/Window with JavaScript





Home
Donate
Contact
Link to Us
No Spam Policy
Privacy Policy
Topics
Site Map

Getting Started
Web Design
Search Engines
Revenue Making
Domains
Web Hosting
Blogging
JavaScripts
PHP
Perl / CGI
HTML
CSS
.htaccess / Apache
Newsletters
General
Seasonal
Reviews
FAQs
Wizards

 

 
Free webmasters and programmers resources, scripts and tutorials
 
HowtoHaven.com: Free How-To Guides
 
Site Design Tips at thesitewizard.com
Find this site useful?
Please link to us.