javascript - Can Response.Redirect and OnBeforeUnload play nice together? - Stack Overflow

Does anyone know how to detect in a OnBeforeUnload event that the server side code sent a Response.Redi

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?

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?

Share Improve this question edited Jun 11, 2021 at 11:23 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 3, 2009 at 21:01 Nathaniel ReinhartNathaniel Reinhart 1,1831 gold badge8 silver badges21 bronze badges 1
  • Response.Redirect sends back a 200 (meaning the page) when you're in an UpdatePanel. I suppose you could intercept the response and change it via an HTTP Module. – Keltex Commented Apr 3, 2009 at 21:35
Add a ment  | 

3 Answers 3

Reset to default 6

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?";
  }
};

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...

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 pleted, allow the redirect in the Javascript code.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744889534a4599319.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信