Rushabh Y Shah
Rushabh Y Shah

Reputation: 23

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

Upvotes: 2

Views: 2880

Answers (2)

Snickfire
Snickfire

Reputation: 512

You can try this:

  if (performance.navigation.type == 1) {
    location.href='https://www.google.com';
  } else {
    console.log( "Not reloaded");
  }

Upvotes: 1

Manikant Gautam
Manikant Gautam

Reputation: 3581

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();

Upvotes: 3

Related Questions