diff --git a/core/misc/form.es6.js b/core/misc/form.es6.js index 96fc990f7c26b4ac136f60d72b9b89932595a13c..8f535dac8aeae83943097b4fc787f498b21918dd 100644 --- a/core/misc/form.es6.js +++ b/core/misc/form.es6.js @@ -265,28 +265,30 @@ * @fires event:formFragmentLinkClickOrHashChange */ const handleFragmentLinkClickOrHashChange = (e) => { - let $target; - + let url; if (e.type === 'click') { - $target = e.currentTarget.location ? $(e.currentTarget.location.hash) : $(e.currentTarget.hash); + url = e.currentTarget.location ? e.currentTarget.location : e.currentTarget; } else { - $target = $(`#${location.hash.substr(1)}`); + url = location; } + const hash = url.hash.substr(1); + if (hash) { + const $target = $(`#${hash}`); + $('body').trigger('formFragmentLinkClickOrHashChange', [$target]); - $('body').trigger('formFragmentLinkClickOrHashChange', [$target]); - - /** - * Clicking a fragment link or a hash change should focus the target - * element, but event timing issues in multiple browsers require a timeout. - */ - setTimeout(() => { - $target.focus(); - }, 300, $target); + /** + * Clicking a fragment link or a hash change should focus the target + * element, but event timing issues in multiple browsers require a timeout. + */ + setTimeout(() => $target.trigger('focus'), 300); + } }; + const debouncedHandleFragmentLinkClickOrHashChange = debounce(handleFragmentLinkClickOrHashChange, 300, true); + // Binds a listener to handle URL fragment changes. - $(window).on('hashchange.form-fragment', debounce(handleFragmentLinkClickOrHashChange, 300, true)); + $(window).on('hashchange.form-fragment', debouncedHandleFragmentLinkClickOrHashChange); /** * Binds a listener to handle clicks on fragment links and absolute URL links @@ -294,6 +296,6 @@ * because clicking such links doesn't trigger a hash change when the fragment * is already in the URL. */ - $(document).on('click.form-fragment', 'a[href*="#"]', debounce(handleFragmentLinkClickOrHashChange, 300, true)); + $(document).on('click.form-fragment', 'a[href*="#"]', debouncedHandleFragmentLinkClickOrHashChange); }(jQuery, Drupal, Drupal.debounce)); diff --git a/core/misc/form.js b/core/misc/form.js index b5689db611696e1d9ddd7c40d721114375a87005..7e32a31a34f382bac76bef7bd1dac7b87800ad60 100644 --- a/core/misc/form.js +++ b/core/misc/form.js @@ -126,22 +126,26 @@ }; var handleFragmentLinkClickOrHashChange = function handleFragmentLinkClickOrHashChange(e) { - var $target = void 0; - + var url = void 0; if (e.type === 'click') { - $target = e.currentTarget.location ? $(e.currentTarget.location.hash) : $(e.currentTarget.hash); + url = e.currentTarget.location ? e.currentTarget.location : e.currentTarget; } else { - $target = $('#' + location.hash.substr(1)); + url = location; + } + var hash = url.hash.substr(1); + if (hash) { + var $target = $('#' + hash); + $('body').trigger('formFragmentLinkClickOrHashChange', [$target]); + + setTimeout(function () { + return $target.trigger('focus'); + }, 300); } - - $('body').trigger('formFragmentLinkClickOrHashChange', [$target]); - - setTimeout(function () { - $target.focus(); - }, 300, $target); }; - $(window).on('hashchange.form-fragment', debounce(handleFragmentLinkClickOrHashChange, 300, true)); + var debouncedHandleFragmentLinkClickOrHashChange = debounce(handleFragmentLinkClickOrHashChange, 300, true); + + $(window).on('hashchange.form-fragment', debouncedHandleFragmentLinkClickOrHashChange); - $(document).on('click.form-fragment', 'a[href*="#"]', debounce(handleFragmentLinkClickOrHashChange, 300, true)); + $(document).on('click.form-fragment', 'a[href*="#"]', debouncedHandleFragmentLinkClickOrHashChange); })(jQuery, Drupal, Drupal.debounce); \ No newline at end of file