How do i close a javascript window?

It may be needed that you need to give a close link in the popup you created. The window.close () method could be used to close a window. However, there are certain security restrictions for using the close() method.

The close method closes only windows opened by JavaScript using the open method. If you attempt to close any other window, a confirm message is displayed, asking the user to choose whether the window is to be closed or not. If you have the reference to a window, you can use the reference to the window to close.

For example:


popup_window = window.open("");

.....

popup_window.close ();


You can close the current window using self.close (); For example:


<a href="self.close ()">Close this Windowa>


Sample Code for window.close()


<html>
<head>
   <title>JavaScript Window Close Example title>
head>
<script type="text/javascript">
   function popuponclick()
   {
      my_window = window.open("",
       "mywindow","status=1,width=350,height=150");

      my_window.document.write('

The Popup Window

'
); } function closepopup() { if(false == my_window.closed) { my_window.close (); } else { alert('Window already closed!'); } } script> <body> <p> <a href="javascript: popuponclick()">Open Popup Windowa> p> <p> <a href="javascript: closepopup()">Close the Popup Windowa> p> body> html>

See the code above in work in the Link below.

JavaScript Window Close Example 1

Click on the ‘Open Popup window’ link to open the popup first and then click on the ‘Close the Popup window’ link to close the popup.

See Also

  • How to make a web form and get it online quickly
  • JavaScript Button
  • JavaScript Popup Windows
  • Using the window.open method
  • Can JavaScript email a form?
  • From an HTML Form to an Email Inbox
  • How to get the value of a form element : Drop downs and lists
  • How to get the value of a form element : check box and radio button
  • How to get the value of a form element using JavaScript
  • Using JavaScript to access form objects when there are multiple forms

What works, what doesn’t, in a nutshell

How do i close a javascript window?

Photo by Nathan Fertig on Unsplash

How can I close a window with JavaScript?

It’s interesting how often this question comes up. It can be answered very quickly. As the MDN says,

Scripts may not close windows that were not opened by script

What does this statement really say?

First: Opening and Closing Windows.

Windows are opened with

window.open(); — full syntax here

Windows are closed with

window.close(); — full syntax here
(but not always!)

This is where the MDN statement comes into play. A simple example should clear this up.

Example:

I’ve created two pages, Page1.html (the parent window) and Page2.html (the child window.)

Since I did not specify the size of the window, it will open as a new tab in the browser. This all works for popup windows as well.

The JavaScript functions are to,

  1. Close the child window.
  2. Close the parent. Hmm.

How do i close a javascript window?

Parent

How do i close a javascript window?

Child

Running the example

Run Page1.html and click “Open child”. A child window opens as separate window as a tab.

How do i close a javascript window?

Child opens as a tab

Go back to the parent and click “Close child” and the child window closes. Viola!

How do i close a javascript window?

Now, on the parent, click “Close me”. Nothing happened. Bummer!
Actually something did happen. I tried to close it two ways. window.close() and self.close(). See the console.log();

How do i close a javascript window?

Window can close itself (or can it?)

So a window can’t close itself.

Hold on just a minute!

According to the MDN it can close itself, under the right conditions.

Add the following button and closeMe() function to Page2.html (child.)

How do i close a javascript window?

Additions to child

Running the example

Run Page1.html and click “Open child”. A child window opens as separate window as a tab, just as before.

Now click the “Close me” in the child. It closes. Viola!

That window closed itself. Observations below.

Conclusion:

  • Since we opened the child using a script, and had a reference handle to that child (in the variable “child”), by the MDN statement, we were able to close it using the handle to the window.close(); (child.close();)
  • Since the parent was not opened with window.open(); it cannot be closed using window.close() or self.close().
  • Since the child window was opened with script, window.open(); it can close itself.

So the statement,

Scripts may not close windows that were not opened by script.

Makes more sense now.

I hope this simple example has cleared a few thing up.

Thanks for reading!

Hold on. One last comment.

If the parent is refreshed, it loses the reference to the child and the parent cannot close the child. Help!

Try it

  1. Open Page1.html and click “Open child”.
  2. Refresh the parent, Page1.html and click “close Child”, fail!

Solution

You will notice I had a variable called “childname”. If we store that on the page, we can use it to get a reference again!

Below, in Page1.html, I added a text field (could be a hidden field), removed the closeMe() function since that was an epic fail! and modified the closeChild() function to look and see if the reference was lost. If so, use the window’s name stored on the form.

How do i close a javascript window?

Observe closeChild()

Running the Example

  1. Open Page1.html and click “Open child”, then click “Close child”. Still works.
  2. Open Page1.html and click “Open child”. Refresh Page1.html, click “Close child” on Page1.html. Child closes. Yay!

Happy coding!

How do I close a popup in JavaScript?

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

Can you close browser with JavaScript?

As of 2017, there is no way to close a web browser tab or window using JavaScript. Modern security standards do not permit this behavior.

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 specific tab in JavaScript?

You can use window. close() method to close the current browser tab/window in JavaScript.