Problem with Form Focus

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bradwiseathome@hotmail.com

    #1

    Problem with Form Focus


    I focus on controls in a web form like this:

    <script language="javas cript">
    var focusControl = null;
    if (document.getEl ementById)
    focusControl = document.getEle mentById('txtAr ea1');
    else if (document.all)
    focusControl = document.all['txtArea1'];
    if (focusControl && focusControl.fo cus)
    focusControl.fo cus();
    </script>

    This works on all of my textboxes and textareas except for 2. For them,
    if I put a "alert(focusCon trol.id);" after the "focusControl.f ocus();",
    then after the user clicks Enter the controlg gets focus. But if I
    don't put that alert in there then the address bar (?!) gets focus.

    How can I make this perfect and foolproof? I've looked at my HTML
    thousands of times and the textareas are like all the others that work
    without an alert box.

    Thanks.

  • RobG

    #2
    Re: Problem with Form Focus

    bradwiseathome@ hotmail.com wrote:[color=blue]
    > I focus on controls in a web form like this:
    >
    > <script language="javas cript">[/color]

    The language attribute is depreciated, type is required:

    <script type="text/javascript">

    But that has nothing to do with your issue :-(
    [color=blue]
    > var focusControl = null;
    > if (document.getEl ementById)
    > focusControl = document.getEle mentById('txtAr ea1');
    > else if (document.all)
    > focusControl = document.all['txtArea1'];
    > if (focusControl && focusControl.fo cus)
    > focusControl.fo cus();
    > </script>
    >
    > This works on all of my textboxes and textareas except for 2. For them,
    > if I put a "alert(focusCon trol.id);" after the "focusControl.f ocus();",
    > then after the user clicks Enter the controlg gets focus. But if I
    > don't put that alert in there then the address bar (?!) gets focus.
    >
    > How can I make this perfect and foolproof? I've looked at my HTML
    > thousands of times and the textareas are like all the others that work
    > without an alert box.[/color]

    Are you running this from the page as it loads?

    It may well be that the browser has not finished creating 'txtArea1'
    when the script is called, so focus goes to the address bar. The
    alert gives the browser time to finish the page and do the focus.

    Try running the script using body onload and see if that helps.


    --
    Rob

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Problem with Form Focus

      bradwiseathome@ hotmail.com wrote:
      [color=blue]
      > I focus on controls in a web form like this:
      >
      > <script language="javas cript">[/color]

      <script type="text/javascript">

      see numerous explanations in this group.
      [color=blue]
      > var focusControl = null;
      > if (document.getEl ementById)
      > focusControl = document.getEle mentById('txtAr ea1');
      > else if (document.all)
      > focusControl = document.all['txtArea1'];[/color]

      If you use document.all, use it as a method (parens), not as a collection
      (brackets).
      [color=blue]
      > if (focusControl && focusControl.fo cus)
      > focusControl.fo cus();[/color]

      Regarding feature detection this is a valid approach in most cases, but
      could result in a script error anyway; it only tests whether that host
      object property exists, not if it is a method (property of type "function")
      as required by the Call Operator `(...)'. Therefore, I have developed
      an efficient and easy-to-use method for testing methods some time ago.
      Feel free to use it if you take heed of the License:

      /**
      * @author
      * (C) 2003, 2004 Thomas Lahn &lt;types.js@Po intedEars.de&gt ;
      * Distributed under the GNU GPL v2 and above.
      * @optional Object|string o
      * Object to be determined an method, i.e. a
      * <code>Functio n</code> object assigned as property of
      * another object. May also be a string to be evaluated
      * and so is applicable to unknown properties.
      * @return type boolean
      * <code>true</code> if <code>o</code> is a method,
      * <code>false</code> otherwise.
      * @see #isMethodType()
      */
      function isMethod(m)
      {
      var t;
      (m = eval(m)) && (t = typeof m);
      return (t == "function" || t == "object");
      }

      You would use it here like this:

      if (focusControl && isMethod(focusC ontrol.focus))
      {
      focusControl.fo cus();
      }
      [color=blue]
      > </script>
      >
      > This works on all of my textboxes and textareas except for 2. For them,
      > if I put a "alert(focusCon trol.id);" after the "focusControl.f ocus();",
      > then after the user clicks Enter the controlg gets focus. But if I
      > don't put that alert in there then the address bar (?!) gets focus.[/color]

      You should post the source code of those two textboxes/textareas if the
      below does not help:
      [color=blue]
      > How can I make this perfect and foolproof? [...][/color]

      Whenever forms are involved, try to avoid getElementById( ), document.all()
      aso. and use DOM Level 0 downwards compatible referencing:

      focusControl = document.forms[...].elements[...];

      This is downwards compatible with names and works in current UAs with IDs
      as well, see the W3C DOM Level 2 HTML Specification.
      [color=blue]
      > I've looked at my HTML thousands of times and the textareas are like all
      > the others that work without an alert box.[/color]

      Can't be. However, you should not continue that practice at all: Focusing
      a control with client-side scripting almost always prevents the user from
      scrolling the respective document with non-pointing devices, such as a
      keyboard.


      PointedEars

      Comment

      • Richard Cornford

        #4
        Re: Problem with Form Focus

        Thomas 'PointedEars' Lahn wrote:[color=blue]
        > bradwiseathome@ hotmail.com wrote:[/color]
        <snip>[color=blue][color=green]
        >> focusControl = document.all['txtArea1'];[/color]
        >
        > If you use document.all, use it as a method (parens),
        > not as a collection (brackets).[/color]
        <snip>

        Quite the reverse. There are browsers implementing the - document.all -
        collection exclusively as an object (rather than as a function object)
        so treating the - all - collection as a function will error in those
        environments, while all the browsers where the - all - collection can be
        called also allow it to be treated as an object, and property accessors
        used. And referencing a member of the - all - collection with a property
        accessor should be faster as there are inevitable overheads in a
        function call.

        Generally no collection should be treated as a function, despite the
        fact that doing so is usually effective in IE (and directly proposed in
        much Microsoft documentation [1]). It is a bad habit to ever get into
        and, if done habitually, will become needless barrier to cross-browser
        scripting.

        Richard.

        [1] Microsoft often seem to take the stance that they have won the
        browser war, so it is not particularly surprising to find their
        documentation proposing practices that would act against cross-browser
        coding.


        Comment

        Working...