How do you close window using javascript which is opened by the user with a url?

This web page contains information on how to open & close browser windows using JavaScript.

Opening a new Browser Window

  • Click Here to open a URL in a new browser window using a standard link tag. Notice the property:  target="_blank" in the href tag.  This creates the new window.
    <a href="ex_window_openclose_new.htm" target="_blank">
  • Click Here to open a URL in a new browser window using the JavaScript window.open() command.  
    <a href=
    "javascript: window.open('ex_window_openclose_newfromjs.htm'); void('');">
  • Click Here to open a URL in a new browser window without any toolbars.
    <a href="javascript: window.open('ex_window_openclose_newfromjs.htm','','toolbar=no'); void('');">
    Because I passed a 3rd parameter of 'toolbar=no' the "Back" and "Forward" buttons will not appear.  Once the 3rd parameter has a value the default for the other attributes is turned off.  That is why none of the the other toolbars appear:  location (URL line), status (bottom status toolbar), etc...
  • Click Here to open a URL in a new browser window without any toolbars except the "status" toolbar.  I feel that the status toolbar should always be displayed.  This is a popular style of opening up a new window with some information in it.
    <a href="javascript: window.open('ex_window_openclose_newfromjs.htm','','status=yes'); void('');">
  • Click Here to open a URL in a new browser window with a certain HEIGHT and WIDTH size having no toolbars except the "status" toolbar.  Very popular also!
    <a href="javascript: window.open('ex_window_openclose_newfromjs.htm','','status=yes, width=400,height=400'); void('');">
  • Click Here to open a URL in a new browser window using the MAXIMUM screen size by using a variable loaded with the window properties called "strWinProp".  Click on View Source and look at the init() JavaScript method which is run when the page loads (<body onLoad = "init()">
    <a href="javascript:window.open('ex_window_openclose_newfromjs.htm','',strWinProp);void('');">
  • Click Here to open a new window with a web page that will automatically open a new popup window.
    <script language="javascript">
    window.open('ex_window_popup_wina.htm','','status=yes, width=400,height=400');
    </script>

Closing a Browser Window

The JavaScript window.close() method will close a browser window.  

  • Closing a window
    • If you try to close a window that the user created (IE or Netscape) then the browser will prompt you with something similar to this: "The web page you are viewing is trying to close this window.  Do you want to close this window?"
    • If a new browser window was created by a web page (ex: using window.open() or <a href> with the property target="_blank") then you will not be prompted.  With IE 5.5 & NS 4.7 (maybe older versions to), if the user used the right click and "Open in New Window" then you can close the window without the close window prompt appearing.
  • Warning, this next link will close this browser window.  You may want to bookmark this web page first.  
    Click Here to close this window.  Note: If you created this window by just clicking the link (vs right click and "Open in New Window"), it will prompt you before closing the window.  You can choose "NO" or "YES".
    <a href="javascript: window.close();">
  • Click Here to create a new window that you can then close.  Using standard html link tag.
    <a href="ex_window_openclose_close.htm" target="_blank">
  • Click Here to create a new window using JavaScript that you can then close. 
    <a href="javascript: window.open('ex_window_openclose_close.htm','','status=yes'); void('');">
  • Click Here to load a new URL into this window.  Warning: if you then click on the close link, you will close this browser window.  You can choose "NO" or "YES".
    <a href="ex_window_openclose_close.htm">
  • Closing windows - Frame vs No frame. 
    • Frame vs New Window - Shows how to determine if new window is in a frame or a new window. The file "..._level1.htm" has the logic in a JavaScript function called closeWindow().
 

How do I close a window using JavaScript?

close() method simply close the window or tab opened by the window. open() method. Remember that - You have to define a global JavaScript variable to hold the value returned by window. open() method, which will be used later by the close() method to close that opened window.

How do I close a popup in JavaScript?

self. close(); should do it. That should work from within the popup.

How do I close a current window?

Click Close Current Window on the File menu to close the active debugging information window. This command is equivalent to pressing CTRL+F4. You can also close a debugging information window by clicking the Close button in the upper-right corner of the information window inside the WinDbg window.

How do I close a window in HTML?

The close() method closes a window.