0

I want to execute a function on click browser back or forward button which send a get request and render dropdown items "as early as possible".

How to detect clicking back or forward button?

And what is the best event to use in this case?

Note : One of the problem I faced when I tried to execute that function with many events is page refresh. although the default behavior on click back button is back to the previous page without refresh, What is the reason or explanation for this happening?

I tried to use performance.getEntriesByType("navigation")[0].type === "back_forward" with "showpage" event but it fires very late, also it refreshes the page, in addition it doesn't execute the function every time I click back, the condition is not met sometimes. "popstate" event doesn't work if the previous page is a totally different document.

I expect to execute the function as early as possible without refresh the page on click back or forward button.

I found that question before but the answer was using "hashchange" event but this solution didin't work.

  • You probably need to refactor how the history API is used by your site - see the duplicate I marked for the explanation as this is not a simple topic. – Rory McCrossan Jun 23 '23 at 13:59
  • I tried the solution in the mentioned question but it didn't work. also part of my question is not covered there. @RoryMcCrossan – Omima Khaled Jun 23 '23 at 14:10

0 Answers0