【问题标题】:As an HTTPHandler.ashx finishes ProcessRequest how to close the browser window with code作为 HTTPHandler.ashx 完成 ProcessRequest 如何使用代码关闭浏览器窗口
【发布时间】:2014-10-02 19:13:58
【问题描述】:

我有一个 HTTPHandler,它执行一个小的工作单元。我没有想要显示的“输出”。当我退出子程序时,是否有一种方法可以从代码中关闭浏览器窗口:

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>")

使用 Google Chrome,我只是将代码作为消息返回(不是预期的“操作”)。是否需要将更多内容包装到 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 中工作,那么我将放弃并吐出一个简单的回应。还在这里研究。

【问题讨论】:

    标签: asp.net httphandler


    【解决方案1】:

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

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

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

    【讨论】:

    • @JohnAdams,脚本必须在 html 页面中才能执行。请参阅我的更新答案。
    • 非常感谢。显然更多的是这个问题。请看我的第二次更新。我认为你应该知道我在这方面的立场,我非常感谢你的快速帮助。
    猜你喜欢
    • 2015-07-02
    • 2013-01-08
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多