How do you close a screen in javascript?

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 Window</a>


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('<h2>The Popup Window</h2>');
   }

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

   <p>
      <a href="javascript: closepopup()">Close the Popup Window</a>
   </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

JavaScript provides a variety of methods to perform any operation on a web page effectively. The window.open() method is used to open a tab in the browser. Moreover, the window.close() method can be employed to close the window utilizing JavaScript. That window is already opened by the user in the URL.

This post demonstrates the working and usage of the window.close() method to close the window with JavaScript that is already opened by a URL.

How to Use the window.close() Method in JavaScript?

In JavaScript, the window.close() method is utilized for closing the window that has already been opened by the window.open() method. The working of the window.close() method is described using the following syntax:

Syntax

The window.close() method does not take any parameter, it just closes the existing open tab in the browser.

Note: Some of the browsers do not support the window.close() method if the window.open() method is not employed first to open any webpage.

For a better understanding, the following lines of code are implemented to close windows with the built-in methods of JavaScript.

Code

<html>

<head>

<center><h2 style="color:red"> An example of Window close() method </h2>

<body>

<b> Press the button to open Linuxhint site </b><br>

<button onclick="windowOpen()"> Open Linuxhint </button>

<br><br>

<b> Press the button to close Linuxhint site </b><br>

<button onclick="windowClose()"> Close Linuxhint </button>

</body> </center>

<script>

var newWindow;

function windowOpen() {

newWindow = window.open(

"https://linuxhint.com/declare-variables-different-ways-javascript/",

"_blank", "width=400, height=250"); }

function windowClose() {

newWindow.close(); }

</script> </head> </html>

In the above code:

  • two buttons Open Linuxhint and Close Linuxhint are created.
  • These buttons are associated with the windowOpen() and windowClose() methods to open and close the window tab.
  • URL is provided in the code to access a webpage.

How do you close a screen in javascript?

Output

The output shows the execution of the window.close() method in JavaScript. The link is opened using the “Open Linuxhint” button. When the “Close Linuxhint” button is pressed, the window is closed.

Conclusion

The window.close() method can be used to close a window by utilizing JavaScript that is already opened by a URL. Most of the time, the window.close() method works with the window.open() method. For a better understanding, we have provided a detailed working and usage of the window.close() method alongside. In the provided example, the window is opened first, and then it is closed using the window.close() method in JavaScript.

About the author

How do you close a screen in javascript?

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.

How do you close a page in JavaScript?

JavaScript provides an in-built function named close() to close the browser window that is opened by using window. open() method.

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 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.