Qn from Novice

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • peter

    Qn from Novice

    (i'm new to javascript) I found the following Javascript and is confused by
    its syntax. Why the expression "window.status= input.form[0].value;" is
    valid (i verified this in ie6) and has the same result as the expression
    "window.status= input.form.elem ents[0].value"?

    As fas as I understand, "input.form " represents the form object. And, this
    form object isnt an array. Why we can use array accessing syntax on an
    ordinary object?

    Any expert can help? thx
    =============== =============== =============== =============== =============== =
    =====

    <html><HEAD>
    <!-- *************** *************** *************** *************** **** -->
    <SCRIPT LANGUAGE="JavaS cript">

    function displayFormValu e(input) {
    window.status=i nput.form[0].value;
    // why this syntax can be used instead of "input.form.ele ments[0].value"
    return true;
    }
    </script>
    <!-- *************** *************** *************** *************** **** -->
    </HEAD><body>
    <form name=form1>
    <input name=text1 type=text onKeyUp="return displayFormValu e(this);"
    size=10/>

    </form>
    <br>good to see you
    </body>
    </html>





  • VK

    #2
    Re: Qn from Novice

    There is not such array as form(), but there is forms() array:
    document.forms[i].elements[j]

    displayFormValu e(this) gives you a reference on the input element itself, so
    in your function you just say:
    window.status=i nput.value;
    which is the right syntacs

    Any of other syntacs work in IE only because this browser has been built
    with a very high tolerance to bad syntacs.
    As long as IE can get any rough idea what in the name are trying to do, it
    will re-adjust your code internally in some more descent way, and it will
    execute the result.
    Sometimes it's good, sometimes it's bad (first of all, no guarantee that IE
    will decrypt your intentions properly; secondly, it may produce well-hidden
    sporadic bugs).


    Comment

    • Martin Honnen

      #3
      Re: Qn from Novice



      peter wrote:
      [color=blue]
      > (i'm new to javascript) I found the following Javascript and is confused by
      > its syntax. Why the expression "window.status= input.form[0].value;" is
      > valid (i verified this in ie6) and has the same result as the expression
      > "window.status= input.form.elem ents[0].value"?
      >
      > As fas as I understand, "input.form " represents the form object. And, this
      > form object isnt an array. Why we can use array accessing syntax on an
      > ordinary object?[/color]

      Well what you think is "array accessing syntax" is simply a way to
      access properties of an object in JavaScript, see


      --

      Martin Honnen

      Comment

      Working...