Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Saving data when detecting a window's being shut down etc.

1 view
Skip to first unread message

ayuan1...@gmail.com

unread,
Jan 9, 2014, 11:40:59 AM1/9/14
to
Hello,

I'm a rookie in JavaScript world, I may need some helps please.

The use case is described as following :
I'm trying to make a "token" system for users to editing some shared in common files, so each time, i want to ensure that only one user can "write" a file, others can only "read" this file.

The current implemention's like this:
A token file's generated to manage the access of files. Each file will have in this token file, a unique value 1 or 0 which represents whether it's actually being edited or it's free.

So when an user A askes to edit a free file, the system will show him the edit web form, while in the same time change the file token to 0 (occupied), so that when an user B demands to edit this file, it will not be allowed.


-----------------------------

The question is that:
when the user A wants to leave the edit page without committing any modification, how can the system change the token of this file to 1 (free)?

I read about using the onunload / beforeunload functions. bascically when detecting a page change or quitting the browser, etc, we can optionally show some messages to ask, such as the script below:

<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "Message to show";
}
</script>

the problem is, it's ok if he chooses to stay in the page, but if he does want to leave, how can i free the file (reset the token value in the token file) before quitting the page?

I wrote the following script

window.onunload = function() {
$.get('tokenManager.php', {
... ...
}, function(data)
{
});
}

But it's not working well if we refresh the page/go back to the last visited page, etc.

Any suggestion?


Thanks a lot

Y.L
0 new messages