Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuejs 'beforeunload' event not triggered as expected

I have registered 'beforeunload' event on created hook of the component used by routes of vue router.

I want to call this event handler in order to remove user on browser tab close or browser tab refresh or browser close.

On ComponentA

created (){    
    window.addEventListener('beforeunload', () => {

        this.removeUser()

        return null
     })
}

Smilarly on ComponentB

created (){    
    window.addEventListener('beforeunload', () => {

        this.removeUser()

        return null
     })
}

And my router.js

{
  path: '/staff/call/:session_key',
  name: 'Staff Call',
  component: ComponentA,
  meta: {auth: true}
},
{
  path: '/consumer/call/:session_key',
  name: 'Consumer Call',
  component: ComponentB
},

Here 'beforeunload' event handler is triggered randomly. That is sometimes it get triggered and sometimes not. I count find any pattern when it is triggered and when it is not.

What am I missing here?

like image 337
wrufesh Avatar asked Apr 09 '18 10:04

wrufesh


People also ask

Is Beforeunload deprecated?

Deprecated. Not for use in new websites.

What is the difference between Onbeforeunload and Beforeunload?

One significant difference (other than cancelability) between the onbeforeunload and onunload is that the former is triggered for download links and the latter is not.

What is Beforeunload event?

The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. The default message that appears in the confirmation box, is different in different browsers.

How do I unsubscribe from Beforeunload?

Cancelable: The beforeunload event can be canceled by user interaction: // by https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload#Example window. addEventListener("beforeunload", function(event) { event. preventDefault(); // Cancel the event as stated by the standard.


2 Answers

Edit
I'd guess the most likely culprit then is exactly what @PatrickSteele said. From MDN:

Note: To combat unwanted pop-ups, some browsers don't display prompts created in beforeunload event handlers unless the page has been interacted with; some don't display them at all. For a list of specific browsers, see the Browser_compatibility section.

I'd say it's likely you're seeing inconsistent behavior because you are sometimes not interacting with the page.

This may be a syntax error. created should be a method

created () {    
        window.addEventListener('beforeunload', this.removeUser)  
    },

    methods: {
      removeUser () {
        //remove user here
      }
    }

A fiddle working: https://jsfiddle.net/e6m6t4kd/3/

like image 166
BFalls Avatar answered Oct 10 '22 04:10

BFalls


It's work for me. while do something before reload or close in vue.js

enter image description here

created() {
    window.onbeforeunload = function(){
           return "handle your events or msgs here";
    }
}
like image 4
Abdullah Avatar answered Oct 10 '22 05:10

Abdullah