Maryadi Poipo
Maryadi Poipo

Reputation: 1478

How to detect page is refreshed in vue.js

I would like to detect when page is refreshed or reloaded in vue.js. I have read the question from this post Do something before reload or close in vue.js. It's using window.onbeforeunload. But the problem for me, where do I put this window function ? Could we use another default method from vuejs for detecting the page that is refreshed ? Thank you

Upvotes: 9

Views: 35824

Answers (1)

rubotero
rubotero

Reputation: 853

You can use the created function to call another function. Example:

created() {
  window.addEventListener('beforeunload', this.handler)
},
methods: {
  handler: function handler(event) {
      // Do Something
  }
}

or

created() {
      window.addEventListener('beforeunload', function(event) {
         event.returnValue = 'Write something'
      })
    },

Check it : https://forum.vuejs.org/t/detect-browser-close/5001/8

Upvotes: 14

Related Questions