684Part IIIDocument Objects ReferenceSee Listing (Web hosting compare) 23-4 for an
684Part IIIDocument Objects ReferenceSee Listing 23-4 for an example of an application of the focus()method in concert with theselect()method. Related Items:select()method; onfocusevent handler. select() Returns:Nothing. Compatibility:WinIE3+, MacIE3+, NN2+, Moz1+, Safari1+ Selecting a field under script control means selecting all text within the text object. A typicalapplication is one in which an entry validation script detects a mistake on the part of theuser. After alerting the user to the mistake (via a window.alert()dialog box), the script fin- ishes its task by selecting the text of the field in question. Not only does this action draw theuser s eye to the field needing attention (especially important if the validation code is check- ing multiple fields), but it also keeps the old text there for the user to examine for potentialproblems. With the text selected, the next key the user presses erases the former entry. Trying to select a text object s contents with a click of a button is problematic. One problemis that a click of the button brings the document s focus to the button, which disrupts theselection process. For more ensured selection, the script should invoke both the focus() and the select()methods for the field, in that order. No penalty exists for issuing bothmethods, and the extra insurance of the second method provides a more consistent userexperience with the page. Some versions of WinIE are known to exhibit anomalous (meaning buggy) behavior whenusing the technique of focusing and selecting a text field after the appearance of an alert dia- log box. The fix is not elegant, but it works: inserting an artificial delay via the setTimeout() method before invoking a separate function that focuses and selects the field. Better-behavedbrowsers accept the workaround with no penalty. Selecting a text object via script does nottrigger the same onselectevent handler for thatobject as the one that triggers if a user manually selects text in the field. Therefore, no eventhandler script is executed when a user invokes the select()method. A click of the Verify button in Listing 23-4 performs a validation on the contents of the text box, making sure the entry consists of all numbers. All work is controlled by the checkNumeric() function, which receives a reference to the field needing inspection as a parameter. Becauseof the way the delayed call to the doSelection()function has to be configured, variousparts of what will become a valid reference to the form are extracted from the field s andform s properties. If the validation (performed in the isNumber()function) fails, thesetSelection()method is invoked after an artificial delay of zero milliseconds. As goofy as this sounds, this method is all that IE needs to recover from the display and closure of the alert dialog box. Because the first parameter of the setTimeout()method must be astring, the example assembles a string invocation of the setSelection()function via stringversions of the form and field names. All that the setSelection()function does is focus andselect the field whose reference is passed as a parameter. This function is now generalizableto work with multiple text boxes in a more complex form. Listing 23-4:Selecting a Field
document.formObject.textObject.focus()We recommend high quality webhost to host and run your jsp application: christian web host services.