I want to execute a js function when users click on back/forward button after redirect. I have a half working function below:
detectHistoryEvent();
function detectHistoryEvent() {
console.log(performance.navigation.type); // deprecated
console.log(performance.getEntriesByType("navigation")[0].type); // currently used
if (performance.getEntriesByType("navigation")[0].type && performance.getEntriesByType("navigation")[0].type === 'back_forward') {
themethod(); // method I want to run
}
}
Working Scenario 1:
- On
PAGE 1
user clicks delete button that executesPOST REQUEST
. - If successful, it will redirect to
PAGE 2
. - From
PAGE 2
when user clicks on back button, it will redirect toPAGE 1
. After redirection, it will executethemethod()
because of the function above. This is because the value of navigation isback_forward
.
Not Working Scenario 2:
- On
PAGE 1
user clicks href that executesGET REQUEST
. - If successful, it will redirect to
PAGE 2
. - From
PAGE 2
when user clicks on back button, it will redirect toPAGE 1
. However thethemethod()
is not executed. This is because the value of nagivation isnavigate
just like a normal href click.
Is it possible to check if user clicks on back/forward button even if PAGE 1
executes GET REQUEST
?