Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GodziLaravel's avatar

how to refresh page when "back" button is clicked vue.JS

Hello ,

I'm wondering how to refresh the page when user clicks browser back button ?

thanks

0 likes
3 replies
bobbybouwmann's avatar

So, it's pretty hard to actually refresh the page whenever you click on the back button. However, it's possible with a small work around.

This should work on most browsers

window.onpopstate = function () {
    location.reload()
};

Note: I said most browsers, in some edge cases it might not work. This is the hard part about controlling the history of a browser ;)

3 likes
guhweb's avatar

Thank you bro! This answers is work fine for me!

1 like
SOFIANE270's avatar

this code works perfectly for all browsers

window.addEventListener("pageshow", function(event) {
  var historyTraversal = event.persisted || 
                         (typeof window.performance != "undefined" && 
                          window.performance.navigation.type === 2);
  if (historyTraversal) {
    // Handle page restore.
    window.location.reload();
  }
});

Please or to participate in this conversation.