简体   繁体   中英

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

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.

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();
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM