Event: Use .preventDefault()
in beforeunload
#5626
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
So far, a result of an event handler has been assigned to the
returnValue
of the original event by jQuery. Initially, one could pass a message the browser will then display to the user. Since that got abused a lot, every browser stopped using the provided string and they all now provide a generic message. From the browsers supported in v4, only IE 11 would still display the message.Incidentally, IE 11 is the only browser from our supported ones which respects the value returned from a beforeunload handler attached by
addEventListener
; other browsers do so only for inline handlers, so not setting the value directly shouldn't reduce any functionality.This looks like a good moment to stop passing the message through and just call
event.preventDefault()
without extra checks which is shorter. This used to not work in Chrome but it got implemented in Chrome 119.Unfortunately, it's hard to test this event in unit tests since it blocks page dismissal.
Size difference:
Checklist