1

Is there an event raised, after going to an page via the browsers back button?

Reason: I have a mobile website which shows an loading animation after clicking on a certain link. If the visitor later goes back to this page with the back button, the animation still blocks the whole ui.

Philipp
  • 15,377
  • 4
  • 35
  • 52
  • 1
    This post may help you: https://stackoverflow.com/questions/25806608/how-to-detect-browser-back-button-event-cross-browser – Ankit Shah Apr 02 '19 at 12:39

2 Answers2

1

I don't know of an event like what you're looking for.

A few other options could solve your problem, though.

You could set a unique value in localstorage, and check for it on document ready. If it exists, then hide/turn off your loading animation.

Set it when you start the animation:

window.localStorage.setItem('loadingAnimationStarted', 'true');

Check for your item on document ready:

document.addEventListener('DOMContentLoaded', function(){
  if(localStorage.getItem('loadingAnimationStarted') === 'true') {
    stopAnimation();
  }
}

You could also have the loading animation be turned off when the user navigates away from the page in the first place using the beforeunload event.

Or, you could also tie the loading animation to the completion of a custom event, or promise depending on what is happening behind the scenes.

Steve Meisner
  • 666
  • 9
  • 17
  • 1
    I did something like you suggested, but saved a unix timestamp inside the session var and left an interval running in the background to check the current pages timestamp against this one – Philipp Apr 02 '19 at 13:32
0

You probably set the loading animation while the user is waiting for the page to load correct?

If the user clicks back, he will be redirected to the previous page, which is cached in the client's browser. That's probably why you don't see the loading animation at all, which is good.

If you have different condition that shows the loading animation, maybe consider to change it accordingly to your purpose.

S.vaysrub
  • 19
  • 3
  • 1
    I know that the page cache is the problem, but I don't know how to get around this. Just found the `pageshow` event, which could be what I was looking for.. – Philipp Apr 02 '19 at 12:47