Created
September 28, 2018 12:47
-
-
Save soullivaneuh/bcb10c3fae3034474fcd26b66aafc643 to your computer and use it in GitHub Desktop.
Prevent PageUp and PageDown press in textarea moving website out of the window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @see http://www.competa.com/blog/chrome-bug-pageup-pagedown-textarea-moves-website-window/ | |
// @see https://bugs.chromium.org/p/chromium/issues/detail?id=890248 | |
document.querySelector('textarea').addEventListener('keydown', event => { | |
if (event.key === 'PageUp' || event.key === 'PageDown') { | |
const cursorPosition = event.key === 'PageUp' ? 0 : event.target.textLength; | |
event.preventDefault(); | |
event.target.setSelectionRange(cursorPosition, cursorPosition); | |
} | |
}); |
Neat, I was wondering if I was the only one seeing such an issue. In my case the PGUP/PGDN keys in a textarea
or div[contentEditable]
cause an offscreen drawer to appear. What's surprising about the bug is that it survives hard refreshes - the element remains in its erroneous position no matter the number of page refreshes, until I manually close the drawer. I have been looking for a bug report for a few days to no avail.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've been annoyed by this for years. It's now 2020 in a few days and the bug is still there. It's even worst when you use white-space="pre", the Home key doesn't even work properly. Personally I will try to avoid hacking anything non-fatal which is supposed to work but doesn't.