unsavedChanges()
Warn before leaving unsaved edits.
Try the example
Edit the project note to enable the guard, then save to disable it. After editing, you can use the test link to try leaving. Your browser chooses whether to show a confirmation.
Add it to your project
- Copy the HTML. Place the markup inside the component where this behavior belongs. The preview’s visual classes are website styles; use your own design system.
- Connect the behavior. Copy the JavaScript tab and call its exported
mount(element)after your component’s elements exist. Replace simulated delays and data operations with your own application code. - Clean up. Call the returned cleanup when the component unmounts. It aborts the example’s listeners and releases the helper’s timers, observers or state.
API and lifecycle
Create a guard and call set(true) when edits become unsaved. It attaches beforeunload only while dirty. Read dirty for the current flag and call set(false) after the server confirms a save.
- Interface
set(boolean), dirty, destroy- Lifecycle
- destroy() removes the navigation warning. A client-side route change needs a separate router or Livewire guard; beforeunload does not cover every application navigation.
Accessibility and safety
Implementation notes
Browser navigation only; SPA route guards require separate integration.
The browser controls warning text and may suppress the prompt, especially on mobile. Pair the guard with recovery or autosave for important work.
What to check
Edit a field and try reloading or leaving the page, then save and repeat. Test your SPA router separately and confirm a successful save removes the guard.