Javascript 关闭按钮单击窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3938278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:44:07  来源:igfitidea点击:

Close Window for button click

javascriptasp.net

提问by user428747

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

我一直试图在单击按钮时关闭窗口,但我无法这样做。

I have added javascript window.close()

我已经添加了 javascript window.close()

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

添加在按钮单击事件的代码隐藏页面上都是徒劳的。语言c#vb.net

回答by CodingGorilla

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.net 按钮,你可以这样做:

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

Although that would be a bit pointless. =)

虽然那会有点没有意义。=)

回答by ShaunO

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.

后面代码上的按钮点击事件只处理影响服务器端的代码。关闭浏览器窗口是客户端操作,必须由浏览器上的某些内容调用。这通常通过输入按钮完成,但可以在任何类型的 javascript 事件中使用。

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.

另外值得注意的是,如果此操作不是由用户操作启动的,浏览器可能会阻止此操作。