Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created June 15, 2024 12:12
Show Gist options
  • Select an option

  • Save barneycarroll/e5bd06fa0ae9abeb6a683eb9a95d5250 to your computer and use it in GitHub Desktop.

Select an option

Save barneycarroll/e5bd06fa0ae9abeb6a683eb9a95d5250 to your computer and use it in GitHub Desktop.

Revisions

  1. barneycarroll created this gist Jun 15, 2024.
    35 changes: 35 additions & 0 deletions BeforeUnload.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    function BeforeUnload(){
    const route. = m.route.set
    const handler = e => {
    if(
    e.target.href
    &&
    e.target != '_blank'
    &&
    !confirm(message)
    ){
    // Need to prevent (native behaviour) & stop (Mithril bindings)
    e.preventDefault()
    e.stopPropagation()
    }
    },

    return {
    view: () => {},

    oncreate: ({attrs: {message = 'Are you sure?'}}) => {
    addEventListener('click', handler, true)

    m.route.set = function(){
    if(confirm(message))
    return route.apply(this, arguments)
    }
    },

    onremove: () => {
    removeEventListener('click', handler, true)

    m.route.set = route
    },
    }
    }