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.

  • It's not possible to detect anything more specific than the `unload` event. – Herohtar Jun 23 '19 at 07:07
  • by the way i tried these before https://stackoverflow.com/questions/3261900/how-to-identify-browser-tab-closing-using-javascript-or-jquery https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing https://stackoverflow.com/questions/26011548/how-to-track-browser-refresh-back-tab-close-and-browser-close-event-in-javascrip – MazMaz Developers Jun 23 '19 at 07: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 – MazMaz Developers Jun 23 '19 at 07:16
  • @JaromandaX does not matther. it's just a snip to detect beforeunload event – MazMaz Developers Jun 23 '19 at 07:18
  • @JaromandaX If sendBeacon does just write an example how to detect browser close || redirection to other domain with this please! – MazMaz Developers Jun 23 '19 at 07:21
  • oh, I thought you had that part figured out - sorry, misunderstood – Jaromanda X Jun 23 '19 at 07:29
  • Depending on how your site is set up, you could maybe do something similar to the convoluted solution in [this answer](https://stackoverflow.com/a/13916847/574531). However, there is no way to directly detect whether the browser is closing vs the user just navigating to another page or refreshing the current one. You should probably handle whatever you're doing with the inventory differently instead of depending on it being reset when the page closes. – Herohtar Jun 23 '19 at 07:48

0 Answers0