0

I want to make a page that redirects users when they reload the page clicking the browser icon or F5, but not when they open the page. Is it possible?

Thanks in advance.

Mahmut Acar
  • 713
  • 2
  • 7
  • 26
Matt
  • 25
  • 7
  • 3
    Possible duplicate of [How to redirect a page to another page when refresh at second attempt](https://stackoverflow.com/questions/8622892/how-to-redirect-a-page-to-another-page-when-refresh-at-second-attempt) – Obsidian Age Nov 06 '18 at 23:02
  • Already tryed, but onbeforeunload work when I try to leave the page, and not when I reload it. – Matt Nov 06 '18 at 23:05

1 Answers1

1

Apparently I don't have enough reputation to comment yet, so I will just link to another post here that should give you the solution you are looking for: Check if page gets reloaded or refreshed in Javascript

So based on that answer, you just do something like this...

//check for Navigation Timing API support
if (window.performance) {
  console.info("window.performance works fine on this browser");
}

if (performance.navigation.type == 1) {
 console.info( "This page is reloaded" );
 window.location = "/redirect_url"; // The url you want
} else {
 console.info( "This page is not reloaded");
}
Wes Winder
  • 1,233
  • 1
  • 8
  • 7
  • @Matt What about it doesn't work? If you check the console logs what does it say? – Wes Winder Nov 06 '18 at 23:32
  • The answer from the original comes from this: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigation, which seems to have been deprecated. It is replaced with this https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming/type. What happens if you change the if statement to this 'if (performance.getEntriesByType("navigation")[0].type == "reload")'? – Wes Winder Nov 06 '18 at 23:42
  • I’ve changed the if, but when i reload nothing happens. My console prints “This page is not reloaded” – Matt Nov 07 '18 at 00:07
  • What browser are you using? – Wes Winder Nov 07 '18 at 02:08
  • Google Chrome . – Matt Nov 07 '18 at 06:54
  • Try just typing 'performance.getEntriesByType("navigation")[0].type' into your console. What does it respond with? – Wes Winder Nov 07 '18 at 15:47