-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Adding a js confirmation before navigating away from settings menus #10927
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
fed6b2c
8de9d1d
15603f6
9b372c0
ecb5403
5a91d7d
48796bf
eaa41ce
e50fd06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,31 @@ | ||||||
| /* global wp */ | ||||||
| ( function () { | ||||||
| var form = document.querySelector( 'form[action="options.php"]' ); | ||||||
|
|
||||||
| if ( ! form ) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| var originalFormContent = new URLSearchParams( new FormData( form ) ).toString(); | ||||||
| var __ = wp.i18n.__; | ||||||
|
|
||||||
| function beforeUnloadHandler( event ) { | ||||||
| var currentContent = new URLSearchParams( new FormData( form ) ).toString(); | ||||||
| if ( originalFormContent !== currentContent ) { | ||||||
| event.preventDefault(); | ||||||
| return __( | ||||||
| 'The changes you made will be lost if you navigate away from this page.' | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Add the beforeunload listener only once a field is modified, to avoid | ||||||
| // breaking bfcache. | ||||||
| document.addEventListener( 'change', function () { | ||||||
|
||||||
| document.addEventListener( 'change', function () { | |
| form.addEventListener( 'change', function () { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,7 @@ | |
| ); | ||
|
|
||
| wp_enqueue_script( 'user-profile' ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not enqueue
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we do need to enqueue user-profile as well, re-added it |
||
| wp_enqueue_script( 'wp-admin-unsaved-changes-confirmation' ); | ||
|
|
||
| require_once ABSPATH . 'wp-admin/admin-header.php'; | ||
| ?> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
beforeunloadlistener is added viaaddEventListener(), but the handler only returns a string. Forbeforeunload, the return value from an event listener is ignored in many browsers; you generally need to setevent.returnValue(and typically still callpreventDefault()) to reliably trigger the confirmation dialog. Update the handler to assignevent.returnValueto the translated message (and return it if needed for older compat).