The Wayback Machine - https://web.archive.org/web/20240829104641/https://www.geeksforgeeks.org/how-to-close-window-using-javascript-which-is-opened-by-the-user-with-a-url/
Open In App

How to close window using JavaScript which is opened by the user with a URL ?

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Syntax:

window.close()

Approach:

The steps below demonstrate this approach:

  • Opening a new window using the open() method: First, we need to open a new window using the window.open() method. The current URL can be accessed using the location property of the window object. The target attribute or name value of the window is given as _self. This is important as it makes the URL replace the current page.
  • Close this open window using the close() method: The window.close() method closes the window on which it is called. The window that was opened in the first step is closed by using this method. This works because the window has now been opened by our script instead of the user.

Note: This approach may not work on all browsers due to different implementations of browser security.

Example: The example below demonstrates the above steps:

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

 <h1 style="color: green;">
    GeeksforGeeks
</h1>

<p>
    Click on the button below to
    close the current window.
</p>

<!-- Define the button to 
       close the window -->
<button onclick="return closeWindow();">
    Close Window
</button>

<script type="text/javascript">
    function closeWindow() {

        // Open the new window 
        // with the URL replacing the
        // current page using the
        // _self value
        let new_window =
            open(location, '_self');

        // Close this window
        new_window.close();

        return false;
    }
</script>
</body>

</html>

Output:



Similar Reads

Javascript Window Open() &amp; Window Close() Method
The Javascript Window.Open() method is used to open the web pages into a new window or a new tab. It depends on the browser settings and the values assigned to the parameter. Syntax: window.open(URL, name, specs, replace)Parameters: This method accepts four parameters as mentioned above and described below: URL: It is an optional parameter. It is u
3 min read
Difference between window.location.href, window.location.replace and window.location.assign in JavaScript
Window.location is a property that returns a Location object with information about the document's current location. This Location object represents the location (URL) of the object it is linked to i.e. holds all the information about the current document location (host, href, port, protocol, etc.) All three commands are used to redirect the page t
4 min read
How to detect whether the website is being opened in a mobile device or a desktop in JavaScript ?
Using CSS Media Queries, we can easily know which user is currently viewing our website on which device (using min-width and max-width). It is only limited to styling web pages, but we can control the functionality of the website according to the user's device using the navigator userAgent Property in JavaScript. We can get information about the us
2 min read
How to prevent Body from scrolling when a modal is opened using jQuery ?
In this article, we will learn how we can prevent the body from scrolling when a popup or modal is opened using jQuery. This task can be easily accomplished by setting the value of the overflow property to hidden using the css() method or by adding and removing the class from the body. Table of Content Using the css() method to set the overflow pro
12 min read
How to close current tab in a browser window using JavaScript?
In this article, we will see how to close the current tab in a browser window using JavaScript. window.close() methodTo 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,
1 min read
jQWidgets jqxTextArea opened Property
jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful, optimized, platform-independent, and widely supported framework. The jqxTextArea represents a jQuery Textarea widget that is used to insert text content inside the textbox. The opened property is used to get a value that indicates
2 min read
jQWidgets jqxInput opened Property
jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful, optimized, platform-independent, and widely supported framework. The jqxInput is used to represent a jQuery input widget that contains auto-complete features The opened property is used to set or return a value that indicates whet
2 min read
How to check keyboard is opened or closed in React Native ?
React Native is an open-source UI software framework created by Meta Platforms, Inc. It is used to develop applications for Android, Android TV, iOS, macOS, tvOS, Web, Windows, and UWP by enabling developers to use the React framework along with native platform capabilities. In this article, we will be learning how to check keyboard is opened or cl
2 min read
JavaScript window.close() Method
JavaScript window.close() method, is used for closing a certain window or tab of the browser which was previously opened by the window.open() method. Syntax: window.close();Parameters: This method does not accept any parameters. Example: In this example, at first we will use the window.open() method to open a new window, assign its value to a tempo
2 min read
Convert relative path URL to absolute path URL using JavaScript
Given a relative URL, the task is to convert the relative URL to an absolute URL. Here, the base URL is also given. 2 approaches are discussed here, the first example has the baseURL provided by the user and the second takes it from the URL of the page. Approach 1: Get the relURL and baseURL from user.Use .split() method to split the base and relat
3 min read
How to open URL in a new window using JavaScript?
In HTML, the anchor tag (&lt;a&gt;) 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 a new browser window or a new tab, depending on th
3 min read
How to open URL in same window and same tab using JavaScript ?
Opening a URL in the same window and in the same tab is a common requirement for web developers. This functionality is useful when you want to load a new web page or document into the current browser window and replace the content of the current web page. There are several ways to open URLs in the same window and in the same tab using JavaScript: T
4 min read
Javascript Window Blur() and Window Focus() Method
JavaScript provides various methods to manipulate browser windows, two of which are window.blur() and window.focus(). These methods are particularly useful when managing the user interface and enhancing the user experience by controlling window focus. Javascript Window Blur()Javascript Window Blur() method is used to remove focus from the current w
3 min read
What is URL Locator & what are common schemes used for the URL in HTML ?
Every HTML document on the internet has a unique URL. URL stands for Uniform Resource Locator. The URL is the HTML document's address on the internet. Any file that can be accessed via the internet has a URL, including images, JavaScript files, CSS files, Flash files, and so on. To view an HTML document in a browser, enter the URL of the document i
2 min read
Web Window API | Window locationbar property
In Web API Window.locationbar property returns the object of locationbar, for which we can check the visibility. Syntax: objectReference = window.locationbar Example: Check the visibility. &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window locationbar property &lt;/title&gt; &lt;style&gt; a:focus { background-color: magenta; }
1 min read
Web Window API | Window scrollbars property
In Web APIWindow.scrollbars property returns the object of scrollbars, for which we can check the visibility. Syntax : objectReference = window.scrollbars Example : Check the visibility &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window scrollbars property &lt;/title&gt; &lt;style&gt; a:focus { background-color: magenta; } &lt;/st
1 min read
Web Window API | DOM Window fullscreen property
The Window interface has a fullscreen property which tells whether the window is displayed in fullscreen mode or not. Syntax: BoolVal = windowReference.fullScreen; Return value: Boolean True: If windows is in fullscreen mode. False: If windows are not in fullscreen mode. Example : &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window
1 min read
Web Window API | Window personalbar property
In Web API Window.personalbar property returns the object of personalbar, for which we can check the visibility. Syntax: objectReference = window.personalbar Example: Check the visibility &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window personalbar property &lt;/title&gt; &lt;script type=&quot;text/javascript&quot;&gt; function
1 min read
Web Window API | Window statusbar property
In Web API Window.statusbar property returns the object of statusbar, for which we can check the visibility. Syntax: objectReference = window.statusbar Example: Check the visibility &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window statusbar property &lt;/title&gt; &lt;script type=&quot;text/javascript&quot;&gt; function getvisib
1 min read
Web Window API | Window toolbar property
In HTML Window.toolbar property returns the object of toolbar, for which we can check the visibility. Syntax: objectReference = window.toolbar Example: Check the visibility C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt; &amp;lt;title&amp;gt; Window toolbar property &amp;lt;/title&amp;gt; &amp;lt;script type=&amp;
1 min read
Web Window API | Window menubar property
In HTML Window.menubar property returns the object of menubar, for which we can check the visibility. Syntax: objectReference = window.menubar Example: This example uses Window.menubar property to check the visibility of menubar object. C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt; &amp;lt;title&amp;gt; Window m
1 min read
Difference between window.onkeypress and window.document.body.onkeypress
The onkeypress event can be triggered in JavaScript using both: window.onkeypresswindow.document.body.onkeypress To understand the difference between the two we need to have a look at the HTML DOM (Document Object Model): DOM is a model that represents the HTML document as a tree structure.The tree structure has the root node as a Document object.O
2 min read
Window | Window.requestAnimationFrame() Method
Nowadays, various web development technologies are introduced to create elegant web pages. While talking about elegant web pages, one of the most important things that come to mind is animation. There are many ways to generate them, modern CSS is one of them but JavaScript has always been a power-packed option to do so. The requestAnimationFrame()
3 min read
User to User private Chat App using ReactJS and Firebase | Without socket programming
This article covers how to make a User to User private Chat App using React JS and Firebase (without socket programming). React is a JavaScript framework provided by Facebook and is used to build fully-featured web applications. We will be following the below steps for creating our application: npx create-react-app chat-appcd chat-appNow install al
10 min read
Which tag is used to display additional information when we click to open or close on demand in HTML ?
HTML provides many predefined tags which have some specific functionality for the contribution of developing web pages. In this article, we will learn how to show the additional information when the user clicks to open or close on demand in HTML. This can be simply done with the help of the HTML &lt;details&gt; tag in the document. This tag is used
1 min read
How to sanitize user input to URL with an amp-bind-macro call ?
Introduction: Sometimes you may want to sanities your input to the search URL of the Google, this can be achieved with the help of amp-bind-macro. It can be used to create a sanitized URL dynamically. A sanitized URL can be achieved by simply converting the input to lowercase and replacing the spaces with '%20'. Setup: To use amp-bind-macro in our
2 min read
How to Close List Items with JavaScript?
Closing list items dynamically with JavaScript involves creating a user interface where each list item has a button or an icon that allows the user to remove that particular item from the list. Basically this is commonly seen in task in task management applications shopping lists and other similar UIs where Items can be added and removed by the use
3 min read
Which attribute is used to target the webpage to open into a new window in HTML ?
HTML (Hyper Text Markup Language) plays the most significant role in website development. But when it comes to good user experience and easy navigation then the &lt;a&gt; tag is used. In this article, we will discuss the hyperlink tag, its possible attributes, and how a user can open a web page in a new window in HTML. IntroductionThe target attrib
2 min read
How to Automatically Close Alerts using Twitter Bootstrap ?
All modern web application uses alerts to show messages to the user. These alerts have a close button to close them or can be closed automatically using Twitter Bootstrap. In this post, we will create automatically closing of alerts using twitter Bootstrap. Basic idea is to use jQuery setTimeout() method to close the alert after specified time. Syn
3 min read
Design a close button using Pure CSS
The task is to make the close button using Pure CSS. There are two approaches that are discussed below. Approach 1: Create a &lt;div&gt; inside another &lt;div&gt; which has alphabet 'X' in it which helps in displaying the close button. When the "click" event occurs on 'X', then perform the operation. In this example the outer &lt;div&gt; has been
2 min read