2

I am trying to send some data for analytics on browser close and the visibilitychange event listener is not being triggered on browser close on firefox. This works as expected in chrome.

MDN says this event fires on browser close.

document.addEventListener('visibilitychange', () => {
  if(document.visibilityState === 'hidden') {
    navigator.sendBeacon('http://localhost:3001')
  }
})
17
  • what if you use beforeunload? Note: how can you be sure the event doesn't trigger - perhaps the issue is that document.visibilityState hasn't changed to hidden when you close Firefox - to test, remove the if condition - again, this is just to test if the event fires or not Commented Jun 10 at 6:49
  • your edit: MDN says this event fires on browser close. - but it doesn't say if document.visibility is changed to hidden - why would it? the document is closed, not hidden i.e. The page content is not visible to the user. In practice this means that the document is either a background tab or part of a minimized window, or the OS screen lock is active - no mention of "or the browser has been closed" :p Commented Jun 10 at 6:53
  • visibilitychange works fine in Firefox (version 56+), there has to be something else going on in your code.
    – Teemu
    Commented Jun 10 at 6:56
  • @Teemu - yes - but I refer you to the documentation for document.visibilityState - which has nothing to do with, nor makes ANY mention of visibilitychange EVENT Commented Jun 10 at 7:05

0