Hi, I am using c# registerscript SaveConfirmation alert box to show successfull message after saving data But I want to close the tab of browser as soon as i click alert OK (tab of any browser)
I have already tried window.close() method and also tried from javascript function, but it did not work
so need help to close the tab or any other popup or any other way to close that tab ?
because I don't want customer to be on same page after details is saved
Thank you !
Amit MohantyPosted Jun 30, 2023, 5:20 AM
Instead of trying to close the tab directly, you could consider redirecting the user to a different page. For example, after showing the success message, you can use the following code to redirect the user to a different page:
Deepak RawatPosted Jun 30, 2023, 4:55 AM
JavaScript to handle the click event and close the tab or window.
Brahma Prakash ShuklaPosted Jun 30, 2023, 3:29 AM
In most modern web browsers, closing a browser tab programmatically is not allowed due to security reasons. This behavior is intentionally implemented by browsers to prevent malicious actions.
However, you can use a workaround to close the tab by opening a new window with a blank page and then closing the current window. Here's an example of how you can achieve this using JavaScript within your C# code:
string script = @"
";
Page.ClientScript.RegisterStartupScript(this.GetType(), "SaveConfirmationScript", script);
In the above code, a JavaScript function
closeWindowis defined, which opens a new window with a blank page (window.open('', '_self')) and then closes the current window (newWindow.close()). The function is called when the DOM content is loaded (DOMContentLoadedevent) after displaying the success message usingalert.Please note that this workaround may not work in some scenarios or browser configurations, as it relies on browser behavior. Also, it's important to consider user experience and ensure that closing the tab is appropriate and does not disrupt the user's workflow.