javascript – Detect smartphone back button

Question:

Is there any way to detect when the smartphone 's physical back button is clicked?

I need this to close a modal .

I thought there could be a matching number like on the keyboard and use event.which to detect, but I couldn't find it.

Answer:

What you can do is use the history API ( https://developer.mozilla.org/en-US/docs/Web/API/History_API ). When you open the modal it would be like entering another page, hence the android's back button and also the browser's will be able to return to the previous state when activated. You don't need to know the code for the button or depend on its existence, as the browser also has one.

Scroll to Top