Reputation: 1467
I have an Exit button on the an aspx page that needs to execute some server side code and then close the browser window. The button has an OnClick event and an OnClientClick event. The OnClientClick event calls the method that will should do a postback and then close the window. The code works fine in IE but will not do the postback in Chrome. The window closes in both cases. Here is my code. I'm checking to see which browser it is, but not doing anything different in each case at this point
function closeMainWindow(control) {
var clientIDName = control.name;
if (navigator.userAgent.indexOf('Chrome') != -1) {
__doPostBack(clientIDName, '');
window.open('', '_self', ''); //need this for Chrome
window.close();
return false;
}
else {
__doPostBack(clientIDName, '');
window.open('', '_self', ''); //bypasses warning from IE
window.close();
return false;
}
Here is the code for the button.
<asp:Button ID="btnExit" runat="server" Text="Yes" Width="50px" OnClick="btnExit_Click" OnClientClick="javascript:return closeMainWindow(this);" />
I would appreciate any suggestions on how to make this work in Chrome or if there is a better/different way to execute server side code and then close the browser window.
Upvotes: 0
Views: 4553
Reputation: 39777
You don't have to perform "window.close()" onClientClick, remove that client-side code. Perform a normal postback without "__doPostBack()" and inside of that server-side postback code send "window.close();" back to client via "ClientScript.RegisterStartupScript"
Upvotes: 2