0

Im working on an ecommerce website which has a booking process composed by multiple steps, and I need to run some php code when the user leaves from the booking process ( so closes the browser from any of the steps, or clicks back multiple times, until he leaves the step1 ), I tried using the js unload/beforeunload events, something like:

 var executeAjax = function (evt) {
  jQuery.ajax({
   url: my_php_url,
   async: false
  });
 window.beforeunload = executeAjax
 $(window).unload(executeAjax)

But the problem is that this code gets executed when the user passes from step1->step2->step3 etc... and also when he refreshes the browser, or click back/forward, but I only need that code to be ran when he leaves the booking process ( so the next url he accesses is not step1,step2, it can be external URL's also, not only my website.... or he closes the browser) and I tried several possibilities and none worked.

Does anyone has an idea of how to solve this, I would be grateful. Thanks.

  • In practice this cannot be done. You need to choose appropriate points at which to save state data, because once the user leaves there is no more of your site code executed. – michaelward82 May 15 '14 at 17:01
  • What tasks need to be done once a user leaves? There's not really a way to handle that, so you may need to rethink the way your application is set up in the first place. – WillardSolutions May 15 '14 at 17:17
  • I need to do a request on an external service used on the booking process, in order to destroy the session on their side, so its mandatory to be done, when the user leaves either one of the steps. – user3641644 May 15 '14 at 18:05

0 Answers0