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 Report 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: Comment More infoAdvertise with us Next Article How to close window using JavaScript which is opened by the user with a URL ? shekharsaxena316 Follow Improve Article Tags : JavaScript Web Technologies HTML JavaScript-Questions Similar Reads 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 a 3 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 secur 1 min read How to Open a Link Without Clicking on it using JavaScript? To open a link without clicking on it we can use the onmouseover method of JavaScript. The link will open when the mouse moves over the text. It returns a newly created window, or NULL if the call gets failed. Syntax:window.open( URL, name, Specs )Note: Allow Pop-up of Web Browser. Example 1: URL is 1 min read How to Open URL in New Tab using JavaScript? HTML anchor tag is used to open URLs in a new tab using the target="_blank" attribute. In JavaScript, we have an inbuilt method window.open() which is used to open a new browser window or new tab depending on the browser settings. Using window.open() MethodTo open a new tab, we have to use _blank in 2 min read Javascript Window Open() & 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 3 min read How to use the alert() method in JavaScript ? In this article, we will learn how to use the alert() method in JavaScript. The alert() method is used to show an alert box on the browser window with some message or warning. We can use it as a message or as a warning for the user. Approach: To show an alert on the browser window, we make a button. 2 min read How to Display Changed Browser URL Without Reloading Through alert using JavaScript ? To change the URL in the browser without loading the new page, we can use history.pushState() method and replaceState() method from JavaScript. To display the browser-URL before changing the URL we will use window.location.href in the alert() function and will use again after changing the browsers-U 1 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 U 3 min read How to Get the Current URL using JavaScript? Here are two different methods to get the current URL in JavaScript.1. Using Document.URL PropertyThe DOM URL property in HTML is used to return a string that contains the complete URL of the current document. The string also includes the HTTP protocol such as ( http://).Syntaxdocument.URLReturn Val 1 min read How to Open a Popup on Click using JavaScript ? Opening a Pop-up when the click action is performed is the user-interactive technique. We can use this functionality in messages, or forms to show some error or alert messages before the user moves to the next action.Table of ContentUsing display propertyUsing classList PropertyUsing visibility prop 4 min read Like