Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to redirect browser window back using JavaScript ?
Next article icon

How to close current tab in a browser window using JavaScript?

Last Updated : 02 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see how to close the current tab in a browser window using JavaScript.

window.close() method

To make this, we will use window.close() method.  This method is used to close the window which is opened by the window.open() method. 

Syntax:

window.close();

But accounting for a security feature, that was introduced a few years ago, ordinary JavaScript lost its privilege to close the current tab, just by using this syntax. 

Note: A current window/tab will only get closed if and only if it was created & opened by that script. Means the window.close syntax is only allowed for the window/tab that is created & opened using window.open method. 

Example: This example shows how to open the GeeksforGeeks window and then close it. 

html
<!DOCTYPE html> <html lang="en">  <head>     <title>         How to close current tab in a          browser window using JavaScript?     </title> </head>  <body style="text-align: center;">     <h2 style="color:green;">         GeeksforGeeks     </h2>          <h4 style="color:purple">         Close Tab/Window using JavaScript     </h4>      <button onclick="openWin()">         Click to open GeeksforGeeks website     </button>      <button onclick="closeWin()">         Click here to close the window     </button>      <script>         let myGeeksforGeeksWindow;          function openWin() {             myGeeksforGeeksWindow = window                 .open("https://www.geeksforgeeks.org",                  "_blank", "width=786, height=786");         }          function closeWin() {             myGeeksforGeeksWindow.close();         }     </script> </body>  </html> 

Output:  

close-window

Output



Next Article
How to redirect browser window back using JavaScript ?

S

SohomPramanick
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • How to redirect browser window back using JavaScript ?
    Redirecting a browser window back to the previous page using JavaScript is a common task for web developers looking to enhance user navigation. This guide will cover the simple yet important methods available in JavaScript to achieve this functionality, ensuring a smooth user experience. There are s
    2 min read
  • How to print the content of current window using JavaScript ?
    The task is to print the content of the current window by using the window.print() method in the document. It is used to open the Print Dialog Box to print the current document. It does not have any parameter value. Syntax: window.print()Parameters: No parameters are required Returns: This function
    2 min read
  • How to detect browser or tab closing in JavaScript ?
    Detecting browser or tab closure in JavaScript is essential for preventing data loss or unintended navigation. Using the beforeunload event, developers can prompt users with a confirmation dialog, ensuring they don't accidentally leave a page with unsaved changes or important information. The before
    2 min read
  • How to Stop Browser Back Button using JavaScript ?
    To prevent users from navigating back to the previous page using the browser's back button, there are a few methods that can be implemented. However, it's essential to note that overriding a user's ability to navigate freely can frustrate them, so these techniques should be used cautiously and only
    3 min read
  • How to clear form after submit in Javascript without using reset?
    Forms in JavaScript serve to gather specific user information, essential in various contexts like recruitment or inquiry submissions. While offline forms can be freely distributed, online forms must maintain a consistent structure for all users. After filling out a form, submission is crucial, but d
    2 min read
  • How To Change The Browser To Fullscreen with JavaScript?
    In web development, there may be scenarios where you want to display your web application or a specific element in full-screen mode, covering the entire browser window. This can be useful for creating immersive experiences, such as games, presentations, or video players. JavaScript provides a built-
    2 min read
  • How to open URL in a new window using JavaScript?
    In HTML, the anchor tag (<a>) is used to open new windows and tabs in a very straightforward manner. However, there are situations where you need to achieve the same functionality using JavaScript. This is where the window.open() method becomes useful. The window.open() method is used to open
    3 min read
  • How to get the size of screen, current web page and browser window using jQuery?
    jQuery can be used to get the dimensions of the current page. The dimensions include the screen, viewport and document height, and width. Getting the window dimensions: Window size is the size of the browser window. This is the size that changes when the browser is resized. The windows height and wi
    2 min read
  • How to close window using JavaScript which is opened by the user with a URL ?
    JavaScript does not allow one to close a window opened by the user, using the window.close() method due to security issues. However, we can close a window by using a workaround. The approach to be followed is by opening the current URL using JavaScript so that it could be closed with a script. Synta
    2 min read
  • How to Create Tab Headers using CSS & JavaScript?
    Tab headers are a commonly used user interface element in web development. They provide a way to organize content into distinct sections, allowing users to switch between them easily. Each tab represents a different category or topic, and clicking on a tab displays the corresponding content while hi
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences