attribute name problem

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

    attribute name problem

    This doesn't work:


    <head>
    <!-- some things snipped -->
    <script>
    function display()
    {
    alert("hey");
    }
    </script>
    </head>
    <body>
    <form >
    <input type="button"
    name="display" <!-- Problem -->
    value="Display All"
    onclick="displa y()" />
    </form>
    </body>




    But with one character different, this does:

    <!-- more things snipped -->
    <form >
    <input type="button"
    name="displa" <!-- Problem solved -->
    value="Display All"
    onclick="displa y()" />
    </form>


    The difference is in the name attribute. I can name it "displa",
    "displays", "displat", or "displa*" and it works. But when I call it
    "display" it doesn't work.

    Why?


    Thanks.
    Dennis
  • Lasse Reichstein Nielsen

    #2
    Re: attribute name problem

    ml3p7@yahoo.com (Dennis) writes:
    [color=blue]
    > function display()[/color]
    [color=blue]
    > <input type="button"
    > name="display" <!-- Problem -->[/color]

    The problem is that you are using IE, and IE has decided to make all
    named elements available as properties of the global object (i.e., as
    global variables).

    That means that the global variable "display", which starts out
    pointing to the function, is overwritten with a reference to the input
    element.
    [color=blue]
    > onclick="displa y()" />[/color]

    So this fails because "display" is not a function.
    [color=blue]
    > But when I call it "display" it doesn't work.[/color]

    "doesn't work" is about as useless as an error report can be.
    My *guess* is that you get an error message (or would get one if you
    have error messages enabled, which you should) saying that "display"
    is not a function.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Michael Winter

      #3
      Re: attribute name problem

      On 7 Aug 2004 11:08:58 -0700, Dennis <ml3p7@yahoo.co m> wrote:

      [snip]
      [color=blue]
      > The difference is in the name attribute. I can name it "displa",
      > "displays", "displat", or "displa*" and it works. But when I call it
      > "display" it doesn't work.
      >
      > Why?[/color]

      As I'm sure you're aware, most browsers accept the shortcut form/control
      accessor that looks like:

      document.formNa me.controlName

      In order for this to work, every named form (formName, above) is made a
      property of the document object. Likewise, every named form control is
      made a property of its parent form.

      When the intrinsic event for a form control is executed, the form object
      itself is added, for reasons unknown to me, to the scope chain. This scope
      chain is used to resolve identifiers. As I already mentioned, named form
      controls are added to the form object as properties, so if you use a
      control name as an identifier, it will be found in the form. This is why
      renaming the form control (or the function) produces the more desired
      results: the name you're looking for can't be found in the form object, so
      the browser looks to the global object and finds it there[1].

      It should be noted that this only occurs when the form control is
      contained within a form. If it's by itself, only the control itself, and
      the global object, are part of the scope chain[2].

      Mike

      [1] Apologies for any discrepancies in that description - my knowledge of
      the scope chain is a little fuzzy, particularly as to why the form is
      added.
      [2] Well, that's what I've seen anyway.

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail

      Comment

      • Michael Winter

        #4
        Re: attribute name problem

        On Sat, 07 Aug 2004 20:42:40 +0200, Lasse Reichstein Nielsen
        <lrn@hotpop.com > wrote:
        [color=blue]
        > ml3p7@yahoo.com (Dennis) writes:
        >[color=green]
        >> function display()[/color]
        >[color=green]
        >> <input type="button"
        >> name="display" <!-- Problem -->[/color]
        >
        > The problem is that you are using IE, and IE has decided to make all
        > named elements available as properties of the global object (i.e., as
        > global variables).[/color]

        I thought that at first, but Opera and Mozilla also display the same
        behaviour. See my other post for my explanation. Am I correct?

        [snip]

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail

        Comment

        • Richard Cornford

          #5
          Re: attribute name problem

          Michael Winter wrote:
          <snip>[color=blue]
          > It should be noted that this only occurs when the form
          > control is contained within a form. If it's by itself,
          > only the control itself, and the global object, are
          > part of the scope chain[2].[/color]
          <snip>

          The exact scope chain created by various browsers varies enormously
          (including some that do not modify the normal JS scope chain with only
          the global object on it), and can depend on the context in which the
          element appears. Mozilla has a strong tendency to add all ancestors in
          the DOM tree to the scope chin of elements outside of forms.

          Google for an exchange between Dom Leonard and me with the subject
          "Works in ie and opera not mozilla" for code to examine the scope chain
          in various browsers.

          Richard.


          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: attribute name problem

            "Michael Winter" <M.Winter@bluey onder.co.invali d> writes:
            [color=blue]
            > When the intrinsic event for a form control is executed, the form
            > object itself is added, for reasons unknown to me, to the scope
            > chain.[/color]

            I believe this convenience feature was added in the early Netscape
            versions.
            [color=blue]
            > It should be noted that this only occurs when the form control is
            > contained within a form. If it's by itself, only the control itself,
            > and the global object, are part of the scope chain[2].[/color]

            And the document element. It's only the form element that is missing.

            Which elements is added to the scope chain depends on the browser. At
            least Opera 5+ and Netscape 2 omit the document element from the scope
            chain. Opera 4 has no special scope chain at all, so only the global
            object is visible. Other browsers (Netscape 3+, Mozilla FF, IE 4+)
            has a scope chain consisting of the global object, document, the form
            and the form control. For all but NS4, an input element outside a form
            still has the document object and itself in the scope chain (NS4 can't
            understand input elements outside of a form).

            I haven't checked, e.g., Macintosh browers.
            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Dennis

              #7
              Re: attribute name problem

              >"doesn't work" is about as useless[color=blue]
              >as an error report can be.[/color]

              Yes. It sure is. Please forgive my carelessness.

              "doesn't work" means that the page loads just fine, but when I hit the
              *display* button I get the message "Object doesn't support this
              property or method," and the function is never called. The function,
              defined elsewhere on the page, is supposed to display an array of
              records the user has entered. According to the tutorial I'm using the
              error message doesn't make sense (to me) because I'm using standard
              properties.

              Dennis

              Comment

              • Dennis

                #8
                Re: attribute name problem

                >As I'm sure you're aware, . .

                Actually, this is my first attempt with Javascript, and my second
                attempt with html. I'm definitely a newbie.

                Thanks for the explanation.

                Dennis

                Comment

                Working...