1

Using jQuery is it possible to detect when the server has sent an HTTP 302 redirect to the browser?

Ideally I would be looking for something similar to the code below but in addition to binding to a click event or submit event, I also need to bind to a HTTP redirect event and set inFormOrLink = true;.

var inFormOrLink;
$('a').on('click', function() { inFormOrLink = true; });
$('form').on('submit', function() { inFormOrLink = true; });

I need to detect when the user closes the broswer tab/window or navigates to another website and have found this StackOverflow answer https://stackoverflow.com/a/1632004/1274662 which gets me most of the way there but doesn't deal with situations where the backend application on the server redirects the user to another page within the same site.

An example use case would be when the user logs into my web application, the server sends an HTTP 302 redirect to another page within the same web application which the jQuery code in the StackOverflow answer interprets as the user leaving the web application.

I have found this StackOverflow question Detecting a redirect in jQuery $.ajax? but the OP for this question wants to not follow the redirect whereas I just need to detect that a redirect has been sent.

Community
  • 1
  • 1
Paul H
  • 2,104
  • 7
  • 39
  • 53

0 Answers0