473,783 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

this vs self vs document

Wow
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?
Jul 23 '05 #1
4 8363
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.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?

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.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?


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 <gw*****@agrico reunited.com>
comp.lang.javas cript 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.object name?
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?
for example, i have a form called theform and a link called thelink

i can call
document.thefor m, but not document.thelin k
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/rasterTriangleD OM.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/rasterTriangleD OM.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
2200
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 longer supported in IE6 but I am not sure on how to fix the code so that it will work with IE6. <!--
4
9515
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 log in again all the users have to do is to click the link in the lower part of the window. This causes the login window to show up again. The problem arise if the window that opened this window has been closed in the meantime. To deal with this...
2
2005
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 moveImage(e){ //shift image according to scroll
12
2180
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
1977
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 python where I'm not quit a newbie and I am not really knowlegable. I know just enough to be dangerous and can really screw things up. Any suggestion on improving would be greatly appreciated. However my real question is I would like to run...
6
4902
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
2
1564
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
1363
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
2163
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 : false; isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.