0

We have a site which is having a strange issue, at least based on my understanding of the onbeforeunload method. Basically, when the user leaves a page, either by navigating away from it, closing the tab, or closing the browser, an AJAX call needs to be made which releases a lock.

The method is very simple:

window.onbeforeunload = function(e) {
   $.ajax({...});
}

(the AJAX call is done synchronously)

We have tested this code and found that if the user closes the specific tab (when there is more than one tab open), the AJAX call works perfectly fine. HoWEVER, if the entire browser is closed, then the code in the onbeforeunload function does not work as expected (i.e. the page lock remains). We have tested this in both Chrome and Edge and the same thing happens in both browser.

My understanding was that the onbeforeunload is supposed to fire whenever the page is navigated away from (i.e. clicking a link, closing the tab, or closing the browser). I know there was a caveat about needing to interact with the page at least once, which does happen. I tested it by editing items on the page, and it the issue still persists.

Can anyone offer any insight into why this would be happening? Everything works find if the tab is closed, but the browser remains open, but fails if the browser is closed. Thank you in advance for any information/guidance you can provide.

Dominick
  • 322
  • 1
  • 3
  • 16
  • Note: To combat unwanted pop-ups, some browsers don't display prompts created in beforeunload event handlers unless the page has been interacted with. Moreover, some don't display them at all. Has there been any interaction on the page? – Grumpy Jan 27 '20 at 19:22
  • https://stackoverflow.com/questions/1270395/javascript-onbeforeunload – Grumpy Jan 27 '20 at 19:23
  • Yes, there has been. And the if the tab itself is closed, everything works as expected. But if I do everything the exact same way and instead of closing the tab, I close the entire browser, then it fails. The only different is closing the browser itself vs closing the tab. – Dominick Jan 27 '20 at 19:24
  • How do you release the lock if the users computer vanishes from the network (computer goes into standby, browser crash, computer crash, power outage etc)? – some Jan 27 '20 at 19:33
  • We do have an service that will release the lock after a specified amount of time for such a case. However, it does seem that a co-worker has resolved this issue. I am not sure what he did and I'm trying to find out. If I do, I will update the post for anyone else that may experience this. – Dominick Jan 27 '20 at 21:02

0 Answers0