-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
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,51 @@ | ||
| /** | ||
| * @output wp-admin/js/options.js | ||
| */ | ||
|
|
||
| /** | ||
| * Detects unsaved changes on settings forms and warns users before navigating away. | ||
| * | ||
| * @since 7.0.0 | ||
| */ | ||
| ( function( $ ) { | ||
| var $form, | ||
| originalFormContent, | ||
| isSubmitting = false, | ||
| __ = wp.i18n.__; | ||
|
|
||
| $( function() { | ||
| // Target all settings forms on options pages. | ||
| $form = $( 'form[action="options.php"]' ); | ||
|
|
||
| // Exit if no form is found. | ||
| if ( ! $form.length ) { | ||
| return; | ||
| } | ||
|
|
||
| // Store the original form state. | ||
| originalFormContent = $form.serialize(); | ||
|
|
||
| // Track form submission to avoid false warnings. | ||
| $form.on( 'submit', function() { | ||
| isSubmitting = true; | ||
| } ); | ||
| } ); | ||
|
|
||
| /** | ||
| * Warn the user if they have unsaved changes. | ||
| * | ||
| * The browser will show a native confirmation dialog when the user | ||
| * attempts to leave the page with unsaved changes. | ||
| */ | ||
| $( window ).on( 'beforeunload', function() { | ||
| // Skip warning if form is being submitted or content hasn't changed. | ||
| if ( isSubmitting || ! $form || ! $form.length ) { | ||
| return; | ||
| } | ||
|
|
||
| if ( originalFormContent !== $form.serialize() ) { | ||
| return __( 'The changes you made will be lost if you navigate away from this page.' ); | ||
| } | ||
| } ); | ||
|
Comment on lines
+34
to
+49
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. This will break bfcache. The For example, add a delegated event listener for the |
||
|
|
||
| }( jQuery ) ); | ||
|
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. Let's avoid using jQuery unless we have to. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,7 +54,7 @@ | |
| '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>' | ||
| ); | ||
|
|
||
| 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 |
||
| wp_enqueue_script( 'options' ); | ||
|
|
||
| require_once ABSPATH . 'wp-admin/admin-header.php'; | ||
| ?> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1256,6 +1256,9 @@ function wp_default_scripts( $scripts ) { | |
|
|
||
| $scripts->add( 'language-chooser', "/wp-admin/js/language-chooser$suffix.js", array( 'jquery' ), false, 1 ); | ||
|
|
||
| $scripts->add( 'options', "/wp-admin/js/options$suffix.js", array( 'jquery', 'wp-i18n' ), false, 1 ); | ||
|
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. This handle seems overly generic. |
||
| $scripts->set_translations( 'options' ); | ||
|
|
||
| $scripts->add( 'user-suggest', "/wp-admin/js/user-suggest$suffix.js", array( 'jquery-ui-autocomplete' ), false, 1 ); | ||
|
|
||
| $scripts->add( 'admin-bar', "/wp-includes/js/admin-bar$suffix.js", array( 'hoverintent-js' ), false, 1 ); | ||
|
|
||
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.
You can use
FormDataas an alternative to this jQuery API.