Skip to content

Commit 9b372c0

Browse files
committed
resolving pr comments - using conditional beforeunload
1 parent 15603f6 commit 9b372c0

3 files changed

Lines changed: 24 additions & 45 deletions

File tree

src/js/_enqueues/admin/options.js

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,31 @@
1-
( function ( $ ) {
2-
var $form,
3-
originalFormContent,
4-
isSubmitting = false,
5-
hasListener = false,
6-
__ = wp.i18n.__;
1+
/* global wp */
2+
( function () {
3+
var form = document.querySelector( 'form[action="options.php"]' );
74

8-
function beforeUnloadHandler() {
9-
if ( isSubmitting || ! $form || ! $form.length ) {
10-
return;
11-
}
5+
if ( ! form ) {
6+
return;
7+
}
128

13-
if ( originalFormContent !== $form.serialize() ) {
9+
var originalFormContent = new URLSearchParams( new FormData( form ) ).toString();
10+
var __ = wp.i18n.__;
11+
12+
function beforeUnloadHandler( event ) {
13+
var currentContent = new URLSearchParams( new FormData( form ) ).toString();
14+
if ( originalFormContent !== currentContent ) {
15+
event.preventDefault();
1416
return __(
1517
'The changes you made will be lost if you navigate away from this page.'
1618
);
1719
}
1820
}
1921

20-
function addBeforeUnloadListener() {
21-
if ( ! hasListener ) {
22-
$( window ).on( 'beforeunload.options', beforeUnloadHandler );
23-
hasListener = true;
24-
}
25-
}
26-
27-
function removeBeforeUnloadListener() {
28-
if ( hasListener ) {
29-
$( window ).off( 'beforeunload.options' );
30-
hasListener = false;
31-
}
32-
}
33-
34-
$( function () {
35-
$form = $( 'form[action="options.php"]' );
36-
37-
if ( ! $form.length ) {
38-
return;
39-
}
40-
41-
originalFormContent = $form.serialize();
42-
43-
// Add listener only when form is modified
44-
$form.on( 'change input', function () {
45-
addBeforeUnloadListener();
46-
} );
22+
// Add the beforeunload listener only once a field is modified, to avoid
23+
// breaking bfcache.
24+
document.addEventListener( 'change', function () {
25+
window.addEventListener( 'beforeunload', beforeUnloadHandler );
26+
}, { once: true } );
4727

48-
$form.on( 'submit', function () {
49-
isSubmitting = true;
50-
removeBeforeUnloadListener();
51-
} );
28+
form.addEventListener( 'submit', function () {
29+
window.removeEventListener( 'beforeunload', beforeUnloadHandler );
5230
} );
53-
} )( jQuery );
31+
} )();

src/wp-admin/options-writing.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
'<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
5555
);
5656

57+
wp_enqueue_script( 'user-profile' );
5758
wp_enqueue_script( 'options' );
5859

5960
require_once ABSPATH . 'wp-admin/admin-header.php';

src/wp-includes/script-loader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,8 +1260,8 @@ function wp_default_scripts( $scripts ) {
12601260

12611261
$scripts->add( 'language-chooser', "/wp-admin/js/language-chooser$suffix.js", array( 'jquery' ), false, 1 );
12621262

1263-
$scripts->add( 'options', "/wp-admin/js/options$suffix.js", array( 'jquery', 'wp-i18n' ), false, 1 );
1264-
$scripts->set_translations( 'options' );
1263+
$scripts->add( 'wp-admin-options', "/wp-admin/js/options$suffix.js", array( 'jquery', 'wp-i18n' ), false, 1 );
1264+
$scripts->set_translations( 'wp-admin-options' );
12651265

12661266
$scripts->add( 'user-suggest', "/wp-admin/js/user-suggest$suffix.js", array( 'jquery-ui-autocomplete' ), false, 1 );
12671267

0 commit comments

Comments
 (0)