简体   繁体   中英

How ti detect browser if refresh or close or new tab [on hold]

hey I am counting the time spent by user in the page. My problem is how to detect if the page refresh or is open a new tab

if (window.performance) {
  console.info("window.performance works fine on this browser");
}
  if (performance.navigation.type == 1) {
    console.info( "This page is reloaded" );
  } else {
    console.info( "This page is not reloaded");
  }
  • For handling refresh you can use the window.onbeforeunload and write a function.
  • For new tab you can use the window.onblur and write a function.

Both these hooks are compatible across all the major browsers Chrome, Firefox, Safari, Edge and IE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM