1

I want to call the logout function on the close tab or close the browser only, not on the refresh page.
I tried with the below example code it doing logout on the close tab perfectly but also doing logout if I refresh the page.
I want to prevent logout on the refresh page.

 window.addEventListener('beforeunload', this.logout)
Shubham Sankpal
  • 133
  • 2
  • 10
  • 1
    Hi, You can see here: https://stackoverflow.com/questions/5004978/check-if-page-gets-reloaded-or-refreshed-in-javascript – Yash Maheshwari May 19 '21 at 06:32
  • @YashMaheshwari, I went to that link but it doesn't work for me. I just need to call logout only if we can detect Close Tab OR Close Browser event. – Shubham Sankpal May 19 '21 at 07:00

1 Answers1

0

I added the below code in mounted() function and it works for me

 window.addEventListener("beforeunload", (event) => {
      // on the navigation type checking refresh or close tab/browser for logout

      console.log("performance.navigation.type " + performance.navigation.type);

      if (performance.navigation.type != 1) {
        this.logout();
      }
      return false;
    });
Shubham Sankpal
  • 133
  • 2
  • 10