Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close Window for button click

I have been trying to close the window on button click but am unable to do so.

I have added javascript window.close()

added on the code behind page on button click event all in vain. Language c# or vb.net

like image 712
user428747 Avatar asked Oct 14 '10 23:10

user428747


2 Answers

That needs to be on the client side, try something like this:

<input type="button" id="close" onclick="window.close()" />

If you want an asp.net button you can do:

<asp:Button ID="close" runat="server" OnClientClick="javascript:window.close()" />

Although that would be a bit pointless. =)

like image 128
CodingGorilla Avatar answered Sep 28 '22 07:09

CodingGorilla


The button click event on the code behind only handles code that affects the server side. Closing a browser window is a client side action and must be called by something on the browser. This is usually done with an input button but can be used inside any kind of javascript event.

Here is an example that I pulled out of existing code, the extra calls were used for browser compatibility.

<input type="button" onclick="window.opener=null; window.close(); return false;" />

Also of note, browsers may block this action if it is not initiated by a user action.

like image 33
ShaunO Avatar answered Sep 28 '22 06:09

ShaunO