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.