Prevent vistor go back to previous page
yarn add prevent-back
<script type="module">
import preventBack from 'https://unpkg.com/prevent-back?module'
preventBack((stay, leave) => {
const confirmedToLeave = confirm('Are you sure you want leave?')
if (confirmedToLeave) {
leave()
} else {
stay()
}
})
</script>
import preventBack, {stay, leave} from 'prevent-back'
preventBack(onAttemptToLeave)
import preventBack from 'prevent-back'
preventBack((stay, leave) => {
const confirmedToLeave = confirm('Are you sure you want leave?')
if (confirmedToLeave) {
leave()
} else {
stay()
}
})
stay
and leave
import preventBack, {stay, leave} from 'prevent-back'
document.querySelector('#js-stay-button').addEventListener('click', stay)
document.querySelector('#js-leave-button').addEventListener('click', leave)
preventBack(() => {
document.querySelector('#js-dialog').showModal()
})