Search code examples
asp.nethttphandler

As an HTTPHandler.ashx finishes ProcessRequest how to close the browser window with code


I have an HTTPHandler which performs a little unit of work. I have no "output" I want to display. Is there a method of closing the browser window from the code as I exit the sub:

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
'   unit of work
'   would like to close browser window here
End Sub

Details of the unit of work that are important to the end user are sent by email. Is there a simple way to close the browser window (or tab) with code (without Javascript if possible)?

UPDATE:

I tried this:

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

With Google Chrome, I just get back the code as a message (not the "action" intended). Does more need to be wrapped into Response.Write or did I misunderstand something?

UPDATE No. 2:

I am still researching. Cannot get this to work; even with a Response.Redirect to the following closeWindow.html file:

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

Google chrome debugger gives this error:

scripts may only close the windows that were opened by it

and nothing else happens. So I searched and found this SO thread:

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

Apparently, there is more to this. If I can't get it to work in Chrome, then I will abandon and just spit out a simple response. Still researching here.


Solution

  • Closing the window via JavaScript would be the only way, so you'd have to write the following to Response:

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

    The first statement prevents a prompt on almost all browsers.