document.visibilityState: Values representing the following 4 possible states: hidden: the page is in the background tab or the browser is minimized visible: the page is in the foreground tab prerender: the page performs pre-rendering processing off the screen document. The value of hidden is true unloaded: The page is being unloaded from memory Visibilitychange event: This event is triggered when the document changes from visible to invisible or from invisible to visible.
You can use onbeforeunload and onunload
You can use HTML5’s Page Visibility API
document.visibilityState: Values representing the following 4 possible states:
hidden: the page is in the background tab or the browser is minimized
visible: the page is in the foreground tab
prerender: the page performs pre-rendering processing off the screen document. The value of hidden is true
unloaded: The page is being unloaded from memory
Visibilitychange event: This event is triggered when the document changes from visible to invisible or from invisible to visible.