** This question was marked as a duplicate. I think it is not an exact duplicate. The suggested duplicate answer involves jQuery and I'm not using that here. But the solution helped with my investigation to find the answer.
I have a requirement to be able to clean up some information based on a person's visit to a web page.
When the user clicks on any of the navigation buttons or links on the page or the back button I'd like to update the information before the user is navigated to the new page.
Basically it looks something like this:
function handleHashChange()
{
removeNodes();
}
window.addEventListener("beforeunload", handleHashChange, false);
removeNodes() calls a few REST APIs to update the data.
I initially tried on Chrome using the "hashchange" event first. It looked like it was getting into handleHashChange() and removeNodes() but it didn't successfully complete.
When I changed the event to be "beforeunload", links and buttons seemed to work OK on Chrome, but the back button didn't always work -- it seemed to be aborting before completing, although I did see it work a couple of times.
When I tried on Firefox, it seemed like it got into the beforeunload method but never completed under any of the scenarios. I did not try it on IE.
Any ideas? The removeNodes() method calls a few REST APIs. It should not be super slow, but I'm guessing that it may be slow enough that the browser is aborting it before it has a chance to complete.