Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useBeforeUnload - not preventing unload #27

Open
chrisspiegl opened this issue Apr 11, 2023 · 0 comments
Open

useBeforeUnload - not preventing unload #27

chrisspiegl opened this issue Apr 11, 2023 · 0 comments

Comments

@chrisspiegl
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant