javascriptdombrowser-detectionbrowser-support

How to detect browser support for pageShow and pageHide?


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);
}

Solution

  • if ('onpagehide' in window) {
       window.addEventListener('pagehide', exitFunction, false);
    } else {
       window.addEventListener('unload', exitFunction, false);
    }