Home > Software design >  How to detect if user pressed back button in their mobile (browser) with JS
How to detect if user pressed back button in their mobile (browser) with JS

Time:12-18

I'm developing a PWA and there is a conflict between a library I use for loading and back button

The only thing I need to detect is whether user clicked back button in their mobile (PWA) or not. Then I can handle the rest.

I searched but did not find anything for my case. I don't want to prevent it from happening, I just want to detect that's it. And I think mobile back button and browser back button are the same in PWA

Detect if user clicked back button or not.

CodePudding user response:

detect browser back

window.onhashchange = function() {
 //blah blah blah
}
function goBack() {
    window.location.hash = window.location.lasthash[window.location.lasthash.length-1];
    //blah blah blah
    window.location.lasthash.pop();
}

CodePudding user response:

use popstate on window obj window.addEventListener('popstate', callBackFn);

whenever use will click on back button popstate event will get triggered

  • Related