When a user requests to edit an entry in our CMS we ‘lock’ it so that nobody else can edit it simultaneously, we release the lock when they submit their changes. However we need to handle the case where the user leaves the page through other links… my first attempt is to use jQuery to fire a synchronous $.ajax() call on $(window).unload. This does work, however on the next page the user sees, the entry appears to be locked (assuming they return to the page listing all the entries, perhaps via the back button). A simple refresh of that page shows the entry is ready for editing again.
My guess here would be that for whatever reason the browser is fetching the next page before the ajax request has been completely processed. The question is whether there is a way to ensure things happen in the correct order.
var fire_unload_ajax = true; // certain things set this to false, not relevant
$(window).unload(function() {
if(fire_unload_ajax && $("#reset-entry-lock form").length == 1) {
$.ajax({
url: window.location.href,
async: false,
cache: false,
type: "POST",
data: $("#reset-entry-lock form").serialize()
});
}
});
I solved a similar problem in an app I wrote recently.
I ended up with code in the load event of every page in my app that checked to see if the user still had locks and released them if they were no longer required. I also had code like yours that released locks on an unload event, in case the user was navigating to a page outside my app.
For Opera users (etc) I had intended to have locks automatically expire every minute or so, and do an async postback from my app every 30 seconds to renew the locks. That way, users who navigated elsewhere would have their locks freed even if the unload event was not fired. Unfortunately, my project was cancelled before this was implemented.