简体   繁体   中英

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

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

You can try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM