当HTTPHandler.ashx完成ProcessRequest时如何用代码关闭浏览器窗口

时间:2014-10-02 19:13:59

标签: asp.net httphandler

我有一个HTTPHandler,它执行一些工作单元。我没有"输出"我想要展示。当我退出sub:

时,是否有一种从代码关闭浏览器窗口的方法
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
'   unit of work
'   would like to close browser window here
End Sub

通过电子邮件发送对最终用户重要的工作单元的详细信息。是否有一种简单的方法可以使用代码关闭浏览器窗口(或选项卡)(如果可能,不使用Javascript)?

更新:

我试过了:

context.Response.Write("<script>window.open('', '_self', ''); window.close();<script>")

使用谷歌浏览器,我只是将代码作为消息返回(而不是&#34;动作&#34;意图)。是否需要将更多内容包装到Response.Write中,还是我误解了什么?

更新第2号:

我还在研究。不能让这个工作;即使使用Response.Redirect到以下closeWindow.html文件:

<!doctype html>
<html>
<head>
<title>
</title>
</head>
<body>
<script type='text/javascript'>
    window.open('', '_self', '');
    window.close();
</script>
</body>
</html>

Google Chrome调试器会出现此错误:

scripts may only close the windows that were opened by it

没有其他事情发生。所以我搜索并找到了这个SO线程:

window.close and self.close do not close the window in Chrome

显然,还有更多。如果我无法在Chrome中使用它,那么我将放弃并只是吐出一个简单的响应。还在这里研究。

1 个答案:

答案 0 :(得分:1)

通过JavaScript关闭窗口是唯一的方法,因此您必须将以下内容写入响应:

string closeWindow = @"<html><head></head><body><script>window.open('', '_self', ''); window.close();</script></body></html>";
context.Response.Write(closeWindow);

第一个语句阻止了几乎所有浏览器的提示。