How to detect browser support for pageShow and pageHide?

epascarello picture epascarello · Aug 2, 2011 · Viewed 21.9k times · Source

How can we do JavaScript object detection for pageShow and pageHide handlers?

if( /* pagehide is supported */ ){
   window.addEventListener('pagehide', exitFunction, false);
}
else{
    window.addEventListener('unload', exitFunction, false);
}

Answer

Shaz picture Shaz · Aug 2, 2011
if ('onpagehide' in window) {
   window.addEventListener('pagehide', exitFunction, false);
} else {
   window.addEventListener('unload', exitFunction, false);
}