[Vuejs]-Disable own back button when about to leave the site? (not the browser implemented one)

0👍

You can use window.history.length to track if history stack still has item(s) (length > 0). However this approach only work when user start afresh from a new tab/window. If a user uses existing tab with some browsing history, this method will not work.

The other way you can do is to mimic the history stack for your own app using sessionStorage. Whenever user navigate to a new page, you will push a record in sessionStorage. And when user click your back button you will pop the a record from sessionStorage. So you can check the sessionStorage is empty and a click to back button will trigger an alert or some sort.

Leave a comment