3

As mentioned by LapinLove404 in window.onbeforeunload not working in chrome, it seems that in Chrome inside the beforeunload function, it's not possible to do anything else than changing the confirmation message.

The following code works in FF and IE, but doesn't in Chrome:

<html>
<head>
<script type="text/javascript">
 window.onbeforeunload = function sair() {
  alert("This alert doesn't show up in Chrome");
  return "If you leave this page your data will be lost!";
 }
</script>
</head>
<body>
 <a href="http://www.example.com"> Leave the page </a>
</body>
</html>

How could I do something before the user leaves the page? For example, if I needed to save the page state with an asynchronous call or unset a previously set lock. I think there are many use cases for it, but unfortunately Chrome doesn't make it a trivial task. Any ideas for a workaround?

Community
  • 1
  • 1
Saul Berardo
  • 2,610
  • 3
  • 20
  • 24

2 Answers2

2

The functionality of onbeforeunload is very limited. I think the most you can do is return a message. You can not stop someone from leaving your page. You can't execute any code in an onbeforeunload. You could try onunload for that.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • 1
    I don't want to stop the user to leave the page, but just make an asynchronous call to unset a lock that was set when the user entered the page. – Saul Berardo Mar 29 '12 at 23:12
2

I believe you can set cookies, write to HTML5 storage, etc... But, for security and denial-of-service reasons, you cannot do certain things like prompt the user, set window.location, etc..., things that could deny the user the opportunity to go to the web page they've asked to go to.

Generally, it's not a good design choice to hold an explicit lock on the server while a given user is on a web page and expect that all paths off that page will allow you to clear that lock. There are lockless techniques that can be used to prevent most update issues. One fairly simple way of doing it is to have the client fetch the initial state of the data and hang onto that. When they want to update the data, they resubmit both the original data and the new data. When the server receives both old and new, it can issue a temporary lock, get the current data and see if it matches the old data submitted by the client. If it does, then nobody else has changed the data while the user was editing it. If the old data and the current data don't match, then the data has been modified while the user was editing. If it has been modified, then it's kind of up to the app what to do - and the strategy can be everything from last one to write wins, to merging only changed fields to denying the write because the underlying data was changed and telling the user that they need to look at the new state of the data and reapply any desired edits.

There are other similar techniques involving changeID numbers or revision IDs instead of a copy of the old data.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    I don't want to stop the user to leave the page, but just make an asynchronous call to unset a lock that was set when the user entered the page – Saul Berardo Mar 30 '12 at 02:07
  • In general, it's bad web design to hold a server lock while a user is on a web page and expect that all paths off that page will allow you to clear that lock. You'll have to do some searching and experimenting. I see some posts saying that you can do a synchronous ajax call in beforeunload and others saying you can even do an asychronous call as long as the server is configured correctly and you don't need a response back (because the response will arrive back after the page has been closed). – jfriend00 Mar 30 '12 at 02:19
  • I added an alternate approach (instead of a lock) to my answer. – jfriend00 Mar 30 '12 at 02:47
  • 1
    This technique unhappily doesn't work for my use case. I have a database of people to be interviewed by many interviewers, possibly at the same time. Every time a interview starts, it must be signalized with a lock, so the same person being interviewed will not be chosen by another interviewer simultaneously. If for some reason the interview is aborted (e.g. closing the window) the lock must be unset, so the person can be picked again. – Saul Berardo Mar 30 '12 at 22:34
  • @SaulBerardo - what do you do if the browser crashes, the power for the computer goes out, the browser window is left open for days, etc... ? – jfriend00 Mar 30 '12 at 22:40