prevent-back

gzip size downloads module formats: umd, cjs, and es jsdelivr

Build Status Code Coverage MIT License PRs Welcome

Prevent vistor go back to previous page

Demo

prevent-back

Install

yarn add prevent-back

Usage

<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>

API

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()
})