JavaScript

How to Close the Current Window in JavaScript?

While designing a web page or site, there can be a requirement to close the current tab or window upon some specific action. For instance, closing a particular web page upon a restricted action or managing the site traffic effectively. In such situations, closing the current window in JavaScript assists in ensuring the privacy of the site, thereby making it secure.

This article will discuss the approaches to close the current window in JavaScript.

How to Close the Current Window Using JavaScript?

The current window can be closed with the help of the “window.close()” method. This method closes the currently opened window.

Example 1: Close the Current Window in JavaScript

In this example, the “window.close()” method closes the current window upon the button click after informing the user via the alert dialog box:

<button onclick="myFunction()">Close</button>

<script>

function myFunction(){

alert('The window will be closed')

window.close()

}

</script>

In the above lines of code:

  • Firstly, create a button having an associated “onclick” event redirecting to the stated function.
  • In the JS part of the code, define a function “myFunction()”.
  • In its definition, alert the given message.
  • Lastly, apply the “window.close()” method to close the current window.

Output

In the output, it can be seen that the stated message is displayed as a notification, and the window is closed upon the button click respectively.

If you want to refrain from closing the entire window, then apply the next approach to close the current tab instead.

Example 2: Close the Current Tab in JavaScript

In this example, the “window.close()” method will be used combined with the “confirm()” method. The latter method returns “true” if the user clicks “OK” in the elevated dialogue box:

<button onclick="myFunction()">Close</button>

<script>

function myFunction(){

if (confirm("Do you want to close the Tab?")) {

window.close();

}

}

</script>

According to the given code:

  • Likewise, create a button with an associated “onclick” event invoking the function “myFunction()”.
  • In the JS code, declare the function “myFunction()”.
  • In the function definition, associate the “confirm()” method with the “if” statement to confirm the user regarding the closure of the tab.
  • Lastly, apply the “window.close()” method to close the current tab.
  • Algorithm: This code will function such that the tab will be closed upon triggering “OK” in the dialogue box.

Output

In this output, it is evident that the current tab has been successfully closed upon the triggered button.

Conclusion

The “window.close()” method can close the current window. This method can be utilized to close the currently opened window and the opened tab. The former example notifies the user and closes the current window. The latter example confirms the user and closes the current tab upon hitting “OK”.This article elaborated on the approaches to close the currently opened window in JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.