How To Close Current Window In Javascript
How to Close a Browser Tab/Window with JavaScript
Closing a temporary window after its work is done
How to Shut a Browser Tab/Window with JavaScript
past Christopher Heng, thesitewizard.com
I was asked past a visitor how he could shut 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 spider web folio.
How to Close a Window with JavaScript
To close a window or tab that was opened using JavaScript, call window.shut()
. For example, the following will close the electric current window/tab.
<script blazon="text/javascript">
window.close() ;
</script>
Annotation that window.close()
will merely work nether the following conditions:
-
On all modern browsers, the web page to exist closed must exist the starting time in that window/tab'southward session history. That is, if you hit the Dorsum push on the browser, you lot will non go to a previous page in that window/tab considering there is none.
Yous tin easily accomplish that by opening that page either with JavaScript, for instance, past using
window.open up()
, or through a link that has atarget="_blank"
attribute. You can find example code for the old in the demo below, and the latter in the article on opening links in a new window or tab.If you simply exam your code in an old browser (eg, old versions of Chrome or Firefox, or any version of Cyberspace Explorer), you will cease up with the mistaken impression that it works fine even if the in a higher place atmospheric condition are not true. Newer browser versions impose these restrictions for security (and other) reasons.
-
Modern browsers volition 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 roughshod for such trickery, simply these methods should no longer piece of work in the current versions of Chrome and Firefox.
Demo
Before you click the demo buttons beneath, please annotation the following:
-
The "Open demo" button will open up a window/tab containing this very commodity, although the browser should automatically scroll to showtime of the demo department, since I linked straight to it. (See How to Link to a Specific Line or Paragraph on a Web Page Using HTML, if y'all want to do this.)
-
Although the "Close current window" button appears in both the original window/tab and the newly-opened demo, it may but work in the latter, depending on how you lot reached this article. If yous are reading this in a modernistic browser, endeavor it. That is, click the push in both the demo window (which should close) and this one (which may or may not).
Having said that, yous may non want to click the "Shut current window" push button on the original window in instance you actually succeed, since you volition then have to reload the page to continue reading.
-
Once again, be warned that the window that pops up is identical to the one you are reading (since it's the aforementioned URL), with no visual cue to distinguish between the original and the demo. As such, if y'all don't look at the list of tabs/windows in your browser after clicking, y'all may not realize that you are already looking at the demo. If you lot repeatedly click the "Open demo" button, thinking that nothing has happened, you will end up with multiple windows/tabs containing this article.
That said, the "Close current window" button should work on all the tabs/windows except possibly the original one. And, of course, the usual way you close a tab in your browser will also work.
Source Code for the Demo
The HTML code for the buttons is every bit follows:
<button type="button" onclick="tsw_open_demo_window();">Open demo in a new window/tab</button>
<button type="push button" onclick="window.shut();">Shut electric 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 chosen when the "Open demo" push button is clicked, is:
role tsw_open_demo_window()
{
window.open up( "close-browser-tab-or-window.shtml#demo" );
}
I used a relative URL here, since I'm just opening this same page, merely you tin utilise an absolute 1 (ie, a complete accost, including the "http://" or "https://" portion) if you wish.
Since the "Close current window" button does nothing but call window.shut()
, I placed the JavaScript directly in its onclick
attribute.
Copyright © 2019-2021 Christopher Heng. All rights reserved.
Get more than complimentary tips and articles like this, on web design, promotion, acquirement and scripting, from https://world wide web.thesitewizard.com/.
thesitewizard™ News Feed (RSS Site Feed)
Do you notice this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Only point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You lot 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 whatsoever class.
Related Manufactures
- How to Brand a Web Folio Always Brandish the Current Year
- How to Change Font Sizes with JavaScript
- How to Use JavaScript to Modify a Cascading Style Sheet (CSS) Dynamically
- How to Make a Mobile-Friendly Website: Responsive Design in CSS
- How to Make / Create Your Own Website: The Beginner'south A-Z Guide
- How to Annals Your Own Domain Name
- What To Practice If You Do Not Own Your Website's Domain Name
- The Fine Print in Web Hosting: Resource Usage Limits
New Manufactures
- How to Gear up the Pinnacle of a DIV Relative to a Browser Window (CSS)
- How to Generate the Free Let's Encrypt SSL Document on Your Own (Windows) Computer
- How to Insert Meta Tags into a Web Folio with BlueGriffon
- How to Play a Song (or Some other Audio Clip) from a List on a Website
- How to Draw a Horizontal Line on a Spider web Page with Expression Web
- How to Create a Website Free of Charge
- Why Tin can't I Brand Up Any Domain I Want? Is There a Way to Do Away with a Registrar Altogether?
- What's the Difference Between a Domain Name Registrar and a Spider web Host?
- How to Make a Mobile-Friendly Website: Responsive Pattern in CSS
- What's the Deviation Betwixt a Content Management System (CMS), a Blog, a Web Editor and an Online Site Builder?
Pop Articles
- How to Create a Blog
- How to Make / Create a Website: The Beginner's A-Z Guide
- Tips on Choosing a Skilful Domain Proper name
- Expression Spider web Tutorial: How to Design a Website with Microsoft Expression Web
- Dreamweaver Tutorial: How to Design a Website with Dreamweaver CS6
- BlueGriffon Tutorial: How to Design a Website with BlueGriffon 3
- How to Pattern and Publish Your Website with KompoZer (gratis WYSIWYG spider web editor)
- Free Feedback/Contact Form Wizard
How to Link to This Page
It will appear on your page as:
How to Close a Browser Tab/Window with JavaScript
Copyright © 2019-2021 Christopher Heng. All rights reserved.
thesitewizard™, thefreecountry™ and HowToHaven™ are trademarks of Christopher Heng.
This folio was terminal updated on 21 Apr 2021.
Source: https://www.thesitewizard.com/javascripts/close-browser-tab-or-window.shtml
Posted by: ferrellgially.blogspot.com
0 Response to "How To Close Current Window In Javascript"
Post a Comment