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')
}
})
beforeunload
? Note: how can you be sure the event doesn't trigger - perhaps the issue is thatdocument.visibilityState
hasn't changed tohidden
when you close Firefox - to test, remove theif
condition - again, this is just to test if the event fires or notMDN says this event fires on browser close.
- but it doesn't say ifdocument.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" :pvisibilitychange
works fine in Firefox (version 56+), there has to be something else going on in your code.visibilitychange
EVENT