2

I have a situation where I want to clear out the user Front End session only when the browser is closed if there are some tabs of the same system opened and one of them closed I dont want to do anything, If there is a page refresh i dont want to do anything either, just on browser close or all tabs close, let me show you my code

App.vue

    async created() {
        window.addEventListener("beforeunload", this.handler);
    }
methods:{
    handler(){// eslint-disable-line no-unused-vars
    console.log("here");
      sessionMixin.methods.clearSession();
      return null;
    }
  }

while this does work, but the problem is it is being called on every refresh and tab close which logs the user out and i dont want that,is there a way maybe to differentiate between page refresh/tab close/browser close? or anything else?

uneeb meer
  • 882
  • 1
  • 8
  • 21
  • Capture the close event of the browser in Vue to check if the browser has to be closed. Sure that is supposed to work. That is the least you can do. But not work with close event completely. You can check with the user, not override. It is a know issue with JavaScript not vue. There is a setting now says in browser that allows user to complete the background tasks to be finished before closing the browser process. It has nothing to do with us though – Gary Feb 23 '21 at 11:49
  • Why is `created` async? – evolutionxbox Feb 23 '21 at 11:49
  • @evolutionxbox it contains some asynchronous backend requests. – uneeb meer Feb 23 '21 at 11:51
  • 1
    @Gary you lost me at include event! can you come again? – uneeb meer Feb 23 '21 at 11:52
  • 1
    @uneebmeer I don’t think the browser has the ability to know the difference between a tab close or a reload. The effect is the same either way, except a reload also loads the page again. – evolutionxbox Feb 23 '21 at 11:53
  • @uneebmeer it was a typo while saving. Google autocomplete. Rectified – Gary Feb 23 '21 at 11:53
  • Does this answer your question? [Trying to detect browser close event](https://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event) – evolutionxbox Feb 23 '21 at 11:54
  • What if the user has multiple sessions across multiple tabs in different windows of the same browser? – old greg Feb 23 '21 at 11:58
  • @jimmysumshugar we he can't the data is stored in VUEX so all tabs will have access to same user data – uneeb meer Feb 23 '21 at 12:00
  • @evolutionxbox most of them are jquery solutions or js – uneeb meer Feb 23 '21 at 12:01
  • `window.addEventListener("beforeunload"` is also JS right? – evolutionxbox Feb 23 '21 at 12:03
  • it is sorry for that i meant mostly jquery based – uneeb meer Feb 23 '21 at 13:07

0 Answers0