Skip to content

useBeforeUnload - not preventing unload #27

Open
@chrisspiegl

Description

@chrisspiegl

It looks like the e.preventDefault() is not enough in some browsers (Brave is where I tested).

However, if I also do e.returnValue = '', it does prevent and ask if I really want to close the tab.

This would be an easy fix:

const handleBeforeUnload = (e: BeforeUnloadEvent) => {
// Show alert message only when isPageDirty is true
if (isPageDirty.value) e.preventDefault()
}

const handleBeforeUnload = (e: BeforeUnloadEvent) => {
    // Show alert message only when isPageDirty is true
    if (isPageDirty.value) {
        e.preventDefault()
        e.returnValue = '';
    }
  }

Source: https://community.plumsail.com/t/any-way-to-prevent-user-from-closing-tab-or-showing-a-warning-before-they-close-it-from-within-forms/10261/8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions