0

When a user wants to edit a task the file gets locked. When they save or hit the close button(from the dialog) the file gets unlocked.

However if the user clicks any of these : Back button, forward button, close tab/browser

the file is locked. I have a cleanup scheduler that unlocks all files after X minutes.

However I would love to be able to do one last request to unlock the file so people don't have to wait X minutes till the task unlocks itself becasue someone decided shut their browser down.

I found this

$(window).unload(function ()
    {
        alert('unloaded');
    });

However I have no clue what browser it supports or if it does everything I require it to do.

chobo2
  • 83,322
  • 195
  • 530
  • 832

1 Answers1

4
window.onunload = function () {
   // has to be synchronous so the function doesn't return immediately
   $.ajax({
      async: false, 
      ...
   });
};

Synchronous requests aren't looked upon too highly because they can freeze the UI, but I don't see any other way if you want to implement this specific behavior. I'm sure you could do it several other ways without requiring a synchronous request however.

For example, you could ask the user to save the file when they attempt to close the window:

// use beforeunload to prompt the user
window.onbeforeunload = function () {
   if (!saved) {
       return "Please save the task before exiting!";
   }
};

Also, most browsers support these events. I think Opera is an exception though.

Cristian Sanchez
  • 31,171
  • 11
  • 57
  • 63
  • @CD Sanchez - Is it better to use window.onbeforeunload then the jquery way I posted? Does the above work on all browsers? – chobo2 Jun 19 '11 at 01:32
  • @chobo2: I believe there's some sort of bug when using jQuery's bind with the unload events. Not sure if that's still applicable but it can't hurt to use the sure-fire way (unless you need to assign more than one event handler to the particular event). – Cristian Sanchez Jun 19 '11 at 01:38
  • @CD Sanchez - Would I still put this in document ready? – chobo2 Jun 19 '11 at 01:40
  • @chobo2: I don't think you have to since it's not dependent on the DOM. – Cristian Sanchez Jun 19 '11 at 01:41
  • @CD Sanchez - Too bad with it have so many bugs. Since I read that window.onbeforeunload is not supported yet in Opera. – chobo2 Jun 19 '11 at 01:44
  • 1
    @chobo2: Yup, Opera doesn't support these events for whatever reason. If you really want this behavior I think you could ignore Opera since not that many people use it compared to the other browsers. – Cristian Sanchez Jun 19 '11 at 01:51
  • @CD Sanchez - One problem that I see is that. It comes up with a dialog(at least in firefox) and it still allows them to leave the page.They could just leave as they have nothing to save and I am still stuck with a locked task. – chobo2 Jun 19 '11 at 02:48
  • @CD Sanchez - Ok I don't know what is run but I am at the verge of giving up on it. I tired to use the combination of both your suggestions. I have a global variable that keeps track if a task is open. If it is it should do a sync ajax call and unlock it. If I just have the ajax call an no if statement it works. If I put it around the if statement it does not work. I need some sort of condition around that ajax call as I don't want it called for every little thing(if they go to another page on my site it seems to call this in FF4) – chobo2 Jun 19 '11 at 03:37
  • @chobo2: Well, yes. It makes sense that it allows the users to dismiss the dialog or else malicious pages could stop the users from leaving the page indefinitely. I think you should combine this solution with your existing solution (scheduled unlocking) and it will alleviate the problem since many users will go back and save the task. – Cristian Sanchez Jun 19 '11 at 03:38
  • @ CD Sanchez -Guess you missed my last post above yours :). I tried that. – chobo2 Jun 19 '11 at 03:39
  • @chobo2: Are you sure that the global variable is being changed when the task is open or closed? You should try printing out the variable (in firebug etc) before/after you close the task to make sure that it is changing. Could you paste your code somewhere or give me the URL (if it does not require a login)? – Cristian Sanchez Jun 19 '11 at 03:44