[Vuejs]-VueJS & Laravel, do a post request when user leaves screen

2👍

Run a js function on page close: Run JavaScript code on window close or page refresh?

You’ll want to add the event listener in your ready function in Vue:

....
data:function(){
    return {sentRequest:false}
},
ready:function(){
    window.onbeforeunload = this.leaving;
    window.onblur = this.leaving;
    window.onmouseout = this.leaving;

},
methods:{
    leaving:function(){
        if(!this.sentRequest){
             //do stuff
             this.sentRequest = true;
        }
        return null;
    }
}
....
👤Jeff

Leave a comment