Search code examples
javascriptpage-refresh

Redirect to different page on Browser Refresh Button Click


I want the user to be redirected to a different page on click of the browser refresh button but I am unable to do so. I have tried using onbeforeunload but it does not seem to work for my requirement as it gives a message of Leave and Stay but even that does not seem to be working on chrome. So how can I redirect the user to a different page on click of browser refresh button.

The Code that I tried:

<html>
<body>
<script>
window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};
</script>
</body>
</html>

Regards, Rushabh


Solution

  • You have to check performance.getEntriesByType on page load . here is how you can try so .

    function checkEvt(){
        var evTypep=window.performance.getEntriesByType("navigation")[0].type;
           if (evTypep=='reload'){
             window.location.replace("http://www.stackoverflow.com");
           }
          
    }
    checkEvt();