Method 1. This will cause a postback on button click in response to which we will send some script to close the window.
VB.NET
Private Sub Button1_Click[ByVal sender As System.Object, ByVal e As System.EventArgs] Handles Button1.Click
Response.Write['window.close[];']
End Sub
C#
private void Button1_Click[object sender, System.EventArgs e]
{
Response.Write['window.close[];'];
}
Method 2. This is preferable since there is no postback involved.
Use the Attribute collections i.e use Attributes of button control to add client side JavaScript code
VB.NET
Button1.Attributes.Add['OnClick', 'window.close[];']
C#
Button1.Attributes.Add['OnClick', 'window.close[];'];
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
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.
The steps below demonstrate this approach:
Step 1: 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.
Step 2: 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.
The example below demonstrates the above steps:
Example:
HTML
GeeksforGeeks
Click on the button below to
close the current window.
Close Window
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;
}
Output:
How do you close a HTML page?
The close[] method closes a window.
How do I close a window when I click it?
Microsoft Windows. Click the X at the top-right corner of a window to close it. Nearly all Windows apps have an X at the top-right corner.
How do I close a window button in HTML?
close[]" still works in Chrome while Edge gives a warning that must be confirmed before it will close. On the other hand the solution title="window. open['', '_self', '']; window. close[];" works in both.