I have a web page which a user can download a PDF file via an ASP.NET web handler (\[code\].ashx\[/code\]). It is implemented like the answer in this question. The problem I have is when I do do this \[code\]window.top.location.href = http://stackoverflow.com/questions/14609389/url;\[/code\] in my JavaScript I have no real control over what happens if there is an exception thrown in the handler. The user experience, when everything works correctly, is that they essentially stay on the page they were on and the browser tells them that they can download the PDF file. However, when there is an exception thrown in the handler they are redirected to the URL of the handler and shown a blank page. Here is some example code to make it more clear:JavaScript:\[code\]function openPDF() { var url = GeneratePDFUrl(); window.top.location.href = http://stackoverflow.com/questions/14609389/url;}\[/code\]Handler:\[code\]public override void Process(HttpContext context){ byte[] pdfdata = http://stackoverflow.com/questions/14609389/GetPDFData(); context.Response.ContentType ="application/pdf"; context.Response.AddHeader("content-disposition", "attachment; filename=\"" + GetPDFFileName() + "\""); context.Response.AddHeader("content-length", pdfdata.Length.ToString()); context.Response.BinaryWrite(pdfdata);}\[/code\]The problem happens when GetPDFData() throws an exception. We are doing what we can to prevent GetPDFData() from throwing an exception, but it is generated from user input so we're also handling it here in case there are cases we don't/can't predict which generate an error. Here is one solution I have come up with, but it shows the user error text (instead of a blank page)\[code\]public override void Process(HttpContext context){ try { byte[] pdfdata = http://stackoverflow.com/questions/14609389/GetPDFData(); WritePDF(pdfdata, GetPDFFileName()); // Executes code in example above } catch (Exception e) { context.Response.Clear(); context.Response.Write(GetErrorMessage(e)); }}\[/code\]Ideally I would like to show the user a popup so they stayed on the page they were.