views:

1399

answers:

3

Does anyone know how to detect in a OnBeforeUnload event that the server side code sent a Response.Redirect? I want to prompt a user to keep them from moving away from a page before it's time, but when the server redirects I it shouldn't prompt the user.

I'm working with legacy code that extensively uses Response.Redirect and I'm not interested in changing the way the redirect occurs. Please don't suggest I use X redirect method.

It should be possible to do this based on the response code in the XMLHttpRequest object as a response redirect should send back a 302.

Edit: The Response.Redirect sends back a 200, with a redirect code in the body which then does a window.location.href = new page in the ASP.Net code. Is there any way to get to the server response to determine that this has happened?

Thanks in advance for any help!

A: 

For non-ajax pages, you're out of luck since the page is never sent to the browser, just the 302 header. So there's no opportunity for javascript processing of OnBeforeUnload.

With ASP.NET AJAX a Response.Redirect really isn't doing a 302. It's sending back a message via the XMLHttpRequest and ASP.NET AJAX is simulating a 302. In theory you could intercept this and perform your own processing...

Keltex
Any idea how to intercept it or get to the actual response?
Nathaniel Reinhart
@Nathaniel There may be a way to do it in javascript. or you might create an HTTPModule, intercept the page and look for the window.location.href code and rewrite the javascript... Maybe replace it will a function call to do the equivalent of OnBeforeUnload
Keltex
@Nathaniel: reading your post further... could you just write some javascript to remove OnBeforeUnload event listener before the redirect occurs?
Keltex
@ Keltex That's exactly what I am trying to do. I only want the OnBeforeUnload to be attached while the sever processing is occurring. Unfortunately the PageRequestManager's endRequest callback doesn't fire on a redirect.
Nathaniel Reinhart
A: 

Yes, Response.Redirect has an overload that lets you decide whether or not to terminate the request (raise a ThreadAbortException). If you let the Response finish (pass false as the second parameter); OnBeforeUnload will also run.

However, simply running OnBeforeUnload will not help you in this case, since your client side code (the prompt) will run before a new page can be sent to the browser (and, if you redirect, it will also only be the 302 header and not the real page).

I would use AJAX to query the server, and if it reports the operation to be completed, allow the redirect in the javascript code.

driis
Perhaps I wasn't clear in the original post - I'm looking for a way to avoid the OnBeforeUnload prompt if the server is redirecting. Also, after further investigation, ASP.net AJAX isn't sending a 302, but rather is doing its own window.location.href = ... I need a way to detect this.
Nathaniel Reinhart
+4  A: 

I worked out a solution to it.

When the PageRequestManager is processing the response from the server, it will set the _processingRequest flag. In order to allow response.redirect to pass through, I changed my javascript to check for that. The event now looks like this:

window.onbeforeunload = function() {
  var prm = Sys.WebForms.PageRequestManager.getInstance();
  if (!prm._processingRequest){ 
    return "Are you sure you want to leave?";
  }
};
Nathaniel Reinhart
Cool, I also used _xmlHttpRequest.responseText that is contained in the prm to know the destination of the redirect.
Matias