this vs self vs document

W

Wow

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

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

i can call
document.theform, but not document.thelink

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

WHY? why thelink can not be called from document?
 
G

Grant Wagner

Wow said:
It is clearly stated that link is an object under document. Read this gif
file
http://www.comptechdoc.org/independent/web/cgi/javamanual/objheirarchy.gif

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[indexOfLinkOnPage] 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="document.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(this);">

As for that image, it's old, outdated and actually just wrong. "window" is not
a property of "navigator". If it were, (navigator.window === window) would be
true. It's not. In fact, navigator.window 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.
 
L

Lasse Reichstein Nielsen

Wow said:
when calling a object in an html,

You mean "When referring to ...". In Javascript, the word "call"
should be reserved for what you do to functions :)
should I use self.objectname
this.objectname or document.objectname?

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.objectname",
"window.objectname" 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?
for example, i have a form called theform and a link called thelink

i can call
document.theform, but not document.thelink

As I said, forms are typically avaiable directly as properties of the
document. Links are not.
i can use both this.theform and this.thelink and self.theform and
self.thelink.

You are using IE.
WHY? why thelink can not be called from document?

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
 
L

Lasse Reichstein Nielsen

Wow said:
It is clearly stated that link is an object under document. Read this gif
file
http://www.comptechdoc.org/independent/web/cgi/javamanual/objheirarchy.gif

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top