2006-05-17

Submit forms with Ctrl+Enter

Ever wished commenting something on the web was just a matter of typing some in a text area and hitting Ctrl+Enter to submit it? A few weeks ago (after a period of rather heavy forum and community browsing) I grew wary of the many places where this was not the case, and wrote up a user script to make it so, everywhere, as it's one very nifty feature. Especially when it's become a natural hard-wired browsing behaviour of yours; the surprise when nothing happens is, at best, mildly amusing the first few times, but just annoying in the long run. So, here it is: direct install link and userscripts.org entry, respectively.

At first, I thought I should publish it immediately, but then I thought I would wait for a while and survey how well it worked in practice. After all, scripts written to be run on all pages across the entire web may once in a while end up interfering with things they should not, or mysteriously fail.

It turned out my cautious suspicion was right, and wrong. I have not had it break anything yet, but I have come across at least one poorly written community site where it did not work. You might too, and if it's annoying enough for you on some place you frequent often, you might want to adjust the script to compensate, as I did; the source should be rather straight-forward and readable; just add another case in the on_ctrl_enter function.

In case you do, feel free to drop me a comment with the fix here, and I might try to maintain a catch-all version, mending the web.

What the script does, is to add an event handler to all <textarea> and <input type="text"> tags, which swallows up all Ctrl+Enter events. On encountering one, it runs a potential form onsubmit handler, when in place and does nothing if it said the form was not ready for submission, or submits the form, if it was.

In the vast majority of cases and on all well-behaved, web standards compliant sites, this does just the trick. There are, however, ill-behaved sites where the buttons you believe to be form submit buttons, are in fact not, but rather javascript: links or buttons with javascript onclick handlers that perform some mandated book keeping, updating some required hidden fields in the form, and then performs the submit, which this script will not do, of course. Fortunately, as this kind of bad practice takes additional work to implement (besides requiring a javascript enabled browser to work at all), most sites do not.

The attentive reader might also have realized that the script will not work if you, say, tab away to focus some non-text-entry field prior to hitting Ctrl+Enter or click somewhere in the page first. This is intentional; in part, because my typical use case is "just type away and instantly submit", so it would be a waste listening for events elsewhere, in part, because there might be multiple forms on the page, and there is no other painless straight-forward way of detecting which form has input focus. When listening in on specific text entry widgets, we do know, though.

Share and enjoy!
blog comments powered by Disqus