1

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 trying to send the request back to the server to unlock the record but it's not working in Firefox. The onbeforeunload event is called in script but it's not sending back the request to the server. Any ideas?

<body>
<script>
    window.onbeforeunload = function (e) {
        doUnlock();
    }

function doUnlock() {
    if (navigator.appName == 'Microsoft Internet Explorer') {
        // works in IE (with an IFRAME)
        utilFrame.location.href = "/unlock.do?recordId=" + recordId;
    } else if (navigator.appName == 'Netscape') {
        // None of this works....
        //window.location.href = "/unlock.do?recordId=" + recordId;
        var req = newXMLHttpRequest();
        req.open("GET", "/unlock.do?recordId=" + recordId, true);
        req.send();
    } else {
        // Works for chrome
        window.open("/unlock.do?recordId=" + recordId);
    }
}

</script>
sproketboy
  • 8,967
  • 18
  • 65
  • 95

2 Answers2

3

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.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 1
    Thank you! This was driving me nuts. Being synchronous is not an issue since I don't need to return anything. As long as it's called all is good. – sproketboy Feb 19 '12 at 16:50
  • @DanHoward Consider implementing the XMLHttpRequest method for chrome as well. A popup is extremely annoying, and is probably blocked by the popup-blocker. – Rob W Feb 19 '12 at 17:05
  • I'm developing a web app where a request (asynchronous) is being sent in unload, and it works just fine. – user123444555621 Feb 19 '12 at 17:46
  • also see http://stackoverflow.com/questions/3584288/can-the-unload-event-be-used-to-reliably-fire-ajax-request – user123444555621 Feb 19 '12 at 17:47
  • @Pumbaa80, yeah OK. I don't know of that works on all browsers. – sproketboy Feb 20 '12 at 16:06
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.

abraham
  • 46,583
  • 10
  • 100
  • 152
  • Thanks. That's an interesting approach but that would require 2 timers. On a server the timer would introduce threading issues. – sproketboy Feb 20 '12 at 16:15
  • I actually didn't articulate it very well but you only need one timer on the client. In the backend you can do `if lock_timestamp + 60 > current_timestamp: locked = true` before returning record. – abraham Feb 20 '12 at 18:17