Many new J2EE developers get caught up in focusing on thedetails and nuances of servlets and JSPs and, as a result, may notlearn how to leverage JavaScript. Some may even dismiss it as toomuch hassle, given cross-browser compatibility issues.
For both audiences there's still value in learning at least aminimal amount about client-side scripting. Even learning about justone feature - setting the cursor on the first form field you may have- can bring measurable benefit to your site visitors.
In this month's Journeyman J2EE, we depart from pure J2EEtopics to address this subject, which should be understood by any Webapplication developer. If you're a newcomer to JavaScript, I'llprovide more than enough to get started.
Where's Your Focus?
Here's the motivation for the problem I want to solve: youvisit a Web site that offers a form inviting you to enter some input.No big deal - we see them all the time, right? Now, how do you get tothe point of entering data into that first field?
Do you use your mouse to move the cursor to that first dataentry area? Or are you a keyboard maven, like me, in which case youprefer to tab to such a field? The problem is you may have to hit thetab key several times before you can enter data.
Indeed, it's typical to visit sites with navigationaltoolbars across the top and/or left side of the form, forcing you totab dozens and dozens of times. It's an annoyance, especially ifvisitors to your site know the site designer could have easilyprevented the problem.
If you think that's being pedantic, consider a simplerexample. If you show users a page that clearly is expecting them toenter some data (perhaps a user ID or a search argument, etc.) beforeproceeding, why not put the cursor right on the data entry fieldrather than forcing them to use the mouse or keyboard to get there.It's a usability issue.
There's an incredibly simple solution. Just the tiniest bitof JavaScript is needed, a single line of code, really. This articleshows how to use it and also lays the most basic foundation for usingJavaScript, if you're new to it.
Laying the Foundation
The technique we're talking about involves putting the"keyboard focus" on whatever form input field - a text, password, ortextarea box or even a radio, checkbox, or select field - you wantthe user to enter data into first. The solution is the JavaScript"focus" method.
The JavaScript language is available to any Web page designeron nearly all Web browsers. (Microsoft calls its version of thelanguage JScript. They're nearly the same, especially for thepurposes of this article.) It's a scripting language that can be usedto add features to your Web page that aren't otherwise provided byHTML.
This article shows you the simplest application of this focusmethod. It's pretty straightforward and should work fine in mostinstances. JavaScript is widely supported by most browsers now, andthis particular feature is supported identically in both Netscape'sand Microsoft's versions of the language, so there's no need to worryabout it breaking on different browsers.






