Greasemonkey: focus first input field

I always get annoyed when I end up at sites where the developer (deliberately?) forgot to set the focus to the relevant input field if there is one. IMDB, as great as its content is, for example fails to give that hang-on-to-your-keyboard experience. I assume that usually you don’t want to switch from keyboard to mouse first in order to put the focus in the search field before you can start typing again.

As a remedy a wrote a little Greasemonkey script that focuses the first “text” input field (text, password, textarea) in the first form if the field is visible i.e. inside the view port.

You may ask yourself whether there are no other scripts available that solve this issue. Sure, there are, but their limits were not acceptable for my needs.

  • Search focus will put focus on the search field regardless of where on the page it is, i.e. the input field could be outside the view port and the page will be scrolled down to it. That’s even worse than using your mouse! Besides, it assumes the input field to be named “search”, “q”, or “keywords” in the HTML source code. How can you make such an assumption?
  • Focus on the search does address the view port issue quite nicely, but also falls short on considering all types of input fields.

3 thoughts on “Greasemonkey: focus first input field

Leave a Reply