Skip to content

Adding a js confirmation before navigating away from settings menus#10927

Open
anukasha-mo wants to merge 1 commit intoWordPress:trunkfrom
anukasha-mo:64623-data-loss-prevention
Open

Adding a js confirmation before navigating away from settings menus#10927
anukasha-mo wants to merge 1 commit intoWordPress:trunkfrom
anukasha-mo:64623-data-loss-prevention

Conversation

@anukasha-mo
Copy link

@anukasha-mo anukasha-mo commented Feb 13, 2026

@github-actions
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props anukasha.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment on lines +34 to +49
/**
* 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.' );
}
} );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break bfcache. The beforeunload should only be added once a field is modified. See https://web.dev/articles/bfcache#beforeunload-caution

For example, add a delegated event listener for the change event. For example:

document.addEventListener( 'change', () => {
    window.addEventListener( 'beforeunload', () => {
        // Check if the original form content is not the same as the current form content.
    } );
}, { once: true } );

}
} );

}( jQuery ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid using jQuery unless we have to.

return;
}

if ( originalFormContent !== $form.serialize() ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use FormData as an alternative to this jQuery API.

'<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
);

wp_enqueue_script( 'user-profile' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not enqueue user-profile anymore?


$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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handle seems overly generic.

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

Successfully merging this pull request may close these issues.

2 participants