0

I have a mobile/web app with a sidebar menu. I'm trying to replicate the same behavior that native apps have. When the Sidebar menu is open and user click on back (android) I'd like to avoid the history.back() so the URL never changes and also some callback that allows me to close the menu.

Is this possible? Or at least with progressive web apps?

Enzo
  • 4,111
  • 4
  • 21
  • 33

1 Answers1

0

Try capturing it with onpopstate:

window.onpopstate = function(event) {
  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};

See more here

FcoRodr
  • 1,583
  • 7
  • 15
  • But is not possible to preventDefault. https://stackoverflow.com/a/32432366/1469219 – Enzo Oct 11 '17 at 13:45
  • 1
    @EnZo You have to push something to state on menu openning, e.g: `window.history.pushState(null, null, window.location.href)` – tenbits Oct 11 '17 at 15:14