0

I am having a website where a user submits a form using AJAX. The response is shown in alert whether the form was successfully submitted or the details of the problem. Now since this process is asynchronous, if the user navigates to another page before the response is received, the XMLHttpRequest object that was used for submitting the form is deleted and the user does not get feedback about the status of the submitted form.

So, is it possible to have the XMLHttpRequest object remain active even if the user navigates to another page?

NOTE I am asking this question again as my previous question was marked duplicate but the answers of that question does not solve my problem.

Community
  • 1
  • 1
Blip
  • 3,061
  • 5
  • 22
  • 50
  • 1
    The answer is **no**, it's not possible. When you transition to the new page, the browser gives you a completely fresh context and everything about the old context is forgotten. – Pointy Jul 09 '15 at 15:08
  • Now, if you architect your site as a single-page application, such that all your page transitions are actually brokered by JavaScript that selectively repaints portions of the view, then you're never reloading the main page and so you can get the effect you're looking for. – Pointy Jul 09 '15 at 15:10
  • However it's still the case that if the user types one of your URLs into the address bar and presses `Enter`, you get a fresh page context, and there's really nothing you can do about that. – Pointy Jul 09 '15 at 15:11
  • @Pointy yes a single page application is one of the solution but using ajax to load all pages would result to a deterioration of SEO of the site – Blip Jul 09 '15 at 15:15
  • Yes, that's true. That's just one of the things you have to deal with in making architectural decisions about your web site/web application. – Pointy Jul 09 '15 at 15:16
  • @Pointy not quite nothing. See my answer - many web apps/sites use this functionality as a last line of defence. Whether it's recognised by a useful amount of users is another matter. – Tom Jardine-McNamara Jul 09 '15 at 15:18
  • 1
    @TomJenkins yes you can help the user avoid dropping the pending request. Other things you can do include providing an explicit way to track results of previous activity. It's really a design issue, or rather a constraint of the basic technology that has to be addressed by overall design approach. – Pointy Jul 09 '15 at 15:21
  • @Pointy absolutely agree. Progress bar/"loading" indicators while form request is submitting would all help users to not leave page before request is done. – Tom Jardine-McNamara Jul 09 '15 at 15:25

1 Answers1

1

Short answer: no.

You could use the beforeunload event to have the browser warn the user that something has not completed. Modern browsers don't let you customise the text shown, for obvious reasons.

Demo: http://www.aspneto.com/webblogscontent/uploads/html/showalertbeforeleavepage.html

Tom Jardine-McNamara
  • 2,418
  • 13
  • 24