Skip navigation

Category Archives: Javascript

I was attempting to rewrite our page lock function (designed to prohibit multiple people from submitting changes to a job at once) using jQuery and had a time trying to get an AJAX call to run with the $(window).unload function. 

After much searching and some IRC chat on the #jQuery irc channel, I finally got it. 

//bind action to page unload

$(window).bind(‘beforeunload’,function(){

   $.ajax({

   type: “GET”,
   url: “includes/PageLock.php”,
   cache: false,
   async: false,
   data: “Job=ID, Mode=’unlock'”,

   });
});

The keys here were to set the ‘cache’ to ‘false’ as well as ‘async’.  This does two things:  1) prohibits the browser from caching the page being called and 2) runs the AJAX synchronously, thus freezing the browser until the call is finished.  Now, whenever a user leaves a page, the page can be unlocked.  I also included a modal window that displays a processing dialog so that the user knows what’s going on.