简体   繁体   中英

On browser close, JavaScript does not always call a custom server side method (Asp.Net/C#)

On browser close event, I am trying to call a server side method from JavaScript, to make some database table changes. I am doing this in the “onbeforeunload” event in JavaScript.

Most of the time, it is successfully calling the server side code and implementing the expected database table changes* . But sometimes, the Server side event does not get invoked. *

Does anyone have any experience with a similar requirement OR would be able to provide us any suggestions on how we can make sure that the Server side event gets invoked always without fail?

Aha, we had one of those problems a while back, where we absolutely needed to give our server notification that the window has closed. We noticed that only Firefox would obey requests made inside onbeforeunload . Finally, the solution is as follows:

$(window).unload(function () {
    $.ajax ({
        url: "...",
        data: "(what you want to send)",
        async: false                             // THIS
    });
});

What's more peculiar is that neither window.onunload nor window.onbeforeunload worked for us; not back then at least.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM