0

I'm Trying to call a ajax method on browser close/ site leaving situation.

I already tried many methods such as "beforeunload" as you can see below, but every of them just called when the browser close & any redirection(even redirects to other pages of current website)

Actually i need a method to detect if the user is leaving the whole web site or closing the browser, it's important to calling my ajax before user leaving the site(Not just the current page). it means i do not want to call that method when the user is hanging around in my web site. I just want it when user is leaving whole the site.

 $(window).on('beforeunload', function () { var params = { pId: -1, Qty: -1, StatusQuo: true }; $.ajax({ type: 'POST', cache: false, url: '/api/SiteServices.asmx/UserBasketAction', data: JSON.stringify(params), contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (result) { if (result.d == true) alert("test"); return false; }, error: function (result) { alert(result.status + ' ' + result.statusText) }, complete: function () { alert("sdasdad"); } }); }); 

for more description i want to redeem some reservation in basket from inventory just if user is leaving the reserves in basket & wants to leave the site.

7
  • It's not possible to detect anything more specific than the unload event. Commented Jun 23, 2019 at 7:07
  • by the way i tried these before stackoverflow.com/questions/3261900/… stackoverflow.com/questions/3888902/… stackoverflow.com/questions/26011548/… Commented Jun 23, 2019 at 7:07
  • @JaromandaX this won't help too. i want to detect the redirection page & if it's contain my domain so there is nothing , other wise user is leaving my website in other hand i want to detect browser tab close or F4 Commented Jun 23, 2019 at 7:16
  • @JaromandaX does not matther. it's just a snip to detect beforeunload event Commented Jun 23, 2019 at 7:18
  • @JaromandaX If sendBeacon does just write an example how to detect browser close || redirection to other domain with this please! Commented Jun 23, 2019 at 7:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.