473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

this vs self vs document

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?
Jul 23 '05 #1
4 8339
Wow
It is clearly stated that link is an object under document. Read this gif
file
http://www.comptechdoc.org/independe...jheirarchy.gif
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?

Jul 23 '05 #2
Wow wrote:
It is clearly stated that link is an object under document. Read this gif
file
http://www.comptechdoc.org/independe...jheirarchy.gif
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?


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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #3
"Wow" <a@alexa.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4
"Wow" <a@alexa.com> writes:
It is clearly stated that link is an object under document. Read this gif
file
http://www.comptechdoc.org/independe...jheirarchy.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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SabMan | last post by:
I understand that document.layers is no longer supported in Netscape 7.1 but I am not sure on how to fix the code so that it will work with Netscape 7.1. I understand that document.all is no...
4
by: Iver Erling Årva | last post by:
I have an application that uses a window.open() to open it's own main window where all my programs takes place. I use a timeout so if nothing goes on for 15 minutes the document below is called. To...
2
by: John | last post by:
The following code works OK in IE 6.0 but does not work in Netscape 7. The image does not shift when one scrolls down but stays stationary in Netscape. Please help Thank you John function...
12
by: Ali | last post by:
I have the following web page with a script in it. <html> <head> <title>Make Your Own Objects test</title> <script>
1
by: LenS | last post by:
If this is the wrong place to post this, please advise better place. Otherwise, I have created the following python program and it works. Running on XP. I think I am now at that stage of learning...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
2
by: Bryan | last post by:
Hello all, Can anyone explain when one should use the "document" object and when one should use the "this" object? Also, is the "self" object the same as the "document" or "this" object?
3
by: Aaron Gray | last post by:
Regarding 'self' :- if (self) document.write( "self<br>"); if (self === window) document.write( "self === window<br>"); else if (self == window) document.write( "self == window<br>");
3
by: aashishn86 | last post by:
var weekend = ; var weekendColor = "#e0e0e0"; var fontface = "Verdana"; var fontsize = 1; var gNow = new Date(); var ggWinCal; isNav = (navigator.appName.indexOf("Netscape") != -1) ? true :...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.