0

Can I detect click event when user press hardware back button (button on mobile context menu) using js?

For example, I have two pages:

  • Login
  • Homepage

The homepage has a popup window, currently, when the popup is showing and then user press the back button, browser will be back to login page. But I just want browser close the popup instead of go back to login page in this case.

Is it possible to detect it?

Edit: This question is about the hardware back button on mobile device (android), not the browser back button.

Hung Nguyen
  • 1,026
  • 11
  • 18
  • You may make a special query for the page with the popup, something like `/home?popup=true` and redirect from `home` to `home?popup=true` on popup appear, so clicking back button will make you go back to `home`. – Igor Aug 22 '19 at 08:06
  • Possible duplicate of [How to Detect Browser Back Button event - Cross Browser](https://stackoverflow.com/questions/25806608/how-to-detect-browser-back-button-event-cross-browser) – Adeel Imran Aug 22 '19 at 08:08
  • Hi @AdeelImran, i tried it before create this question, but they discussed about browser back button, i asked about hardware back button on mobile device. – Hung Nguyen Aug 22 '19 at 08:17
  • @Igor but it will reload homepage, right? can i close popup without reload anything? – Hung Nguyen Aug 22 '19 at 08:19
  • 1
    @Hung Nguyen yes, you should just do `$router.push({ query: { popup: true } }` on popup show and browser back button will return you back to `home` route without page reloading. – Igor Aug 22 '19 at 08:37

1 Answers1

0

This is the correct anwser that I got from @Igor:

you should just do $router.push({ query: { popup: true } } on popup show and browser back button will return you back to home route without page reloading

Hung Nguyen
  • 1,026
  • 11
  • 18