[Vuejs]-Vue.js redirect to a new page/component if F5 is pressed

0👍

Ok I found a way to do it on my desktop by using the following code :

var
  is_asked = false;

window.onbeforeunload = 
  function (ev) {
    var e = ev || window.event;
    window.focus();
    if (!is_asked){
      is_asked = true;
      var showstr = "CUSTOM_MESSAGE";
      if (e) {  //for ie and firefox
        e.returnValue = showstr; 
      }
      return showstr; //for safari and chrome
    }
  };

window.onfocus =
  function (ev){
    if (is_asked){
      window.location.href = "http://localhost:8080";
    }
  }

But still no luck on my mobile….

when I use safari on my desktop it works well but when I use safari on mobile I get a blank page again. I think there is something related to window.onbeforeunload not supported by mobile?

0👍

I fixed the issue by using localstorage.

Instead of redirecting to the Home page I stored the form data on the localstorage and everything is fine now – https://www.npmjs.com/package/vue-localstorage

Leave a comment