this vs self vs document

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

    this vs self vs document

    when calling a object in an html, should I use self.objectname
    this.objectname or document.object name?

    for example, i have a form called theform and a link called thelink

    i can call
    document.thefor m, but not document.thelin k

    i can use both this.theform and this.thelink and self.theform and
    self.thelink.

    WHY? why thelink can not be called from document?


  • Wow

    #2
    Re: this vs self vs document

    It is clearly stated that link is an object under document. Read this gif
    file
    Dive into our comprehensive guide to understanding router login processes, IP addresses like 192.168.1.1, 10.0.0.1, and more. Learn how to access and manage your router's settings, check your private IP, and optimize your network using our easy step-by-step guide.

    [color=blue]
    > when calling a object in an html, should I use self.objectname
    > this.objectname or document.object name?
    >
    > for example, i have a form called theform and a link called thelink
    >
    > i can call
    > document.thefor m, but not document.thelin k
    >
    > i can use both this.theform and this.thelink and self.theform and
    > self.thelink.
    >
    > WHY? why thelink can not be called from document?
    >
    >[/color]


    Comment

    • Grant Wagner

      #3
      Re: this vs self vs document

      Wow wrote:
      [color=blue]
      > It is clearly stated that link is an object under document. Read this gif
      > file
      > http://www.comptechdoc.org/independe...jheirarchy.gif
      >[color=green]
      > > when calling a object in an html, should I use self.objectname
      > > this.objectname or document.object name?
      > >
      > > for example, i have a form called theform and a link called thelink
      > >
      > > i can call
      > > document.thefor m, but not document.thelin k
      > >
      > > i can use both this.theform and this.thelink and self.theform and
      > > self.thelink.
      > >
      > > WHY? why thelink can not be called from document?[/color][/color]

      In most user agents, the name of the form is made a property of the document
      object, the names of anchor tags are not (although in IE, the names and ids of
      pretty much everything are made members of the global (window in most Web
      browsers) object. This leads to the sort of confusion you are having.

      The best way to reference objects is by their fully qualified path in the DOM
      hierarchy:

      document.forms['formName'].elements['elementName']

      To access links on the page, you have a variety of options, the most
      cross-browser compatible method is to use the links collection of the document
      object:

      document.links[indexOfLinkOnPa ge] or document.links[nameOrIdOfLink] (note that
      using the name/id may not work in all browsers)

      As for a discussion of self, this and document. Your question seems to
      indicate a fundamental lack of understanding. In simple terms:

      "document" refers to the default HTMLDocument object created by the user agent
      when a page is rendered, typically document is a property of the global
      (window) object

      "self" is also a property of the global (window) object, one which points back
      at the window object such that (window.self === window) should be true (it
      isn't in Internet Explorer, but it is in Gecko-based browsers, Netscape 4.78
      and Opera 7.53). I'm unsure why people use "self". It requires JavaScript to
      step through the object in the DOM you actually want to retrieve a property
      which points back at the object you wanted in the first place. I guess it's
      useful for self-documenting code (pun not intended). I don't know, I never use
      it, when I want the default window object, I use "window".

      "this" always refers to the current object. Outside of any function, "this"
      would refer to the global (window) object such that (this === window) returns
      true. Within functions, "this" can refer to a number of things, but it is
      always the "current object". Example:

      <body onload="documen t.links[0].onclick = onclickHandler; ">
      <script type="text/javascript">
      function onclickHandler( ) {
      alert(
      "'this' is 'window': " +
      (this === window) +
      "\n" +
      "'this' is 'document.links[0]': " +
      (this === document.links[0])
      );
      return false;
      }
      </script>
      <a href="#">'this' will refer to 'document.links[0]'</a><br />
      <a href="#" onclick="return onclickHandler( );">'this' will refer to
      'window'</a>
      </body>

      Same event handler code, same event (onclick) attached on the same type of
      element, but "this" refers to a different thing for each link. If you wanted a
      reference to the link that triggered the event for the second link, you'd
      need:

      <a href="#" onclick="return clickHandler(th is);">

      As for that image, it's old, outdated and actually just wrong. "window" is not
      a property of "navigator" . If it were, (navigator.wind ow === window) would be
      true. It's not. In fact, navigator.windo w is undefined in every user agent I
      tested. You _might_ be able to say that diagram is a representation of the DOM
      hierarchy (which is different from a JavaScript object heirarchy) in Netscape
      4, but I wouldn't even go that far. You'd be best served by simply removing
      any bookmarks you have to that page.

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: this vs self vs document

        "Wow" <a@alexa.com> writes:
        [color=blue]
        > when calling a object in an html,[/color]

        You mean "When referring to ...". In Javascript, the word "call"
        should be reserved for what you do to functions :)
        [color=blue]
        > should I use self.objectname
        > this.objectname or document.object name?[/color]

        No. They are all wrong for some browser.

        The "this" operator can refer to many different objects, depending on
        where you use it. Often it will be either the global object (the same
        referred to by the global variables "self" and "window") or the
        document. In either case, it is better to know which one is used and
        do it explicitly.

        IE is known for making named HTML elements available as global
        variables of the same name (i.e., as "self.objectnam e",
        "window.objectn ame" or plain "objectname "). It will fail in Mozilla,
        so it's a bad choice.

        Netscape, and later Mozilla, makes some named HTML elements available
        as identically named properties of the document object. Some other
        browsers also make some elements available this way (typically forms),
        but there are also exceptions. It's very often also a bad choice.

        That leaves none of the above, so what is a coder to do?
        [color=blue]
        > for example, i have a form called theform and a link called thelink
        >
        > i can call
        > document.thefor m, but not document.thelin k[/color]

        As I said, forms are typically avaiable directly as properties of the
        document. Links are not.
        [color=blue]
        > i can use both this.theform and this.thelink and self.theform and
        > self.thelink.[/color]

        You are using IE.
        [color=blue]
        > WHY? why thelink can not be called from document?[/color]

        Because it isn't there. None of these ways to access the elements are
        part of any standard. However, There are wasy, almost universally
        implemented and standard compliant ways to access both forms and links.

        document.forms['theform']
        and
        document.links['thelink']
        You'll never need to use anything else (and shouldn't either).

        Good luck.
        /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

        • Lasse Reichstein Nielsen

          #5
          Re: this vs self vs document

          "Wow" <a@alexa.com> writes:
          [color=blue]
          > It is clearly stated that link is an object under document. Read this gif
          > file
          > http://www.comptechdoc.org/independe...jheirarchy.gif[/color]

          Don't rely on that image for any exact information. It is, at best, a
          rough sketch of relations between some types of objects in a browser.

          There is no "Language" Object, the image just says that Array, etc., are
          parts of the language. There is a "navigator" object, but it's a child
          of the window object, not the other way around. Perhaps "Navigator" merely
          refers to the browser itself. The children of "Document" are the types
          of elements that are available in collections (like forms and links),
          except that it seems this was written for an early Netscape browser (the
          only ones to have an active document.layers collection).

          So, forget that image. I think it confuzes you more than it helps.

          /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

          Working...