Skip to content Skip to sidebar Skip to footer

Sending A Request Back To Server In Onbeforeunload Event

When a user edits a record in my application I track that they have it 'locked' to prevent another user from editing it until the first user closed the window (or saves). I'm tryin

Solution 1:

Add a space between new and XMLHttpRequest.

On page unload, any pending (AJAX) requests are *cancelled. If you have to send an AJAX request to the server on unload, use a synchronous request (not recommended!!!):

    var req = new XMLHttpRequest();
    req.open("GET", "/unlock.do?recordId=" + recordId, false); // false
    req.send();

I do not recommend the synchronous request, because the user interface blocks until the request has finished. That's not very user-friendly.


Solution 2:

You should look into using a time based lock. When a user starts editing a record lock it with a timestamp. Every 30 seconds send a ping from the user that the record should still be locked. If there is no ping for 60 seconds unlock the record.


Post a Comment for "Sending A Request Back To Server In Onbeforeunload Event"