473,396 Members | 1,749 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,396 software developers and data experts.

When to use "document" and when to use "this"

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?

Sep 23 '06 #1
2 1528
Bryan wrote on 23 sep 2006 in comp.lang.javascript:
Can anyone explain when one should use the "document" object and when
one should use the "this" object?
IMHO:

"window" is the default and top element of the DOM tree.
So: document defaults to window.document.
[However parent.document could mean the framse page.]
"window" is also the general scope of variables.

"this" is the top element of the local DOM scope,
and that could be a <div>, an <a>, etc.
Also, is the "self" object the same as the "document" or "this" object?
"self" seems to be used outside the browser and DOM environment,
say in wscript jscript or serverside jscript,
as general scope of variables, where in a browser "window" is used.

===========

<body style='color:red;'>
<div style='color:green;'
onclick='alert("this: "+this.style.color);
alert("document.body: "+document.body.style.color);'
>Click me</div>
===========

<script type='text/javascript'>

var v = 1;

function show(){
var v = 'Hello';
alert(v) // Hello
alert(this.v) // 1
alert(window.v) // 1
alert(self.v) // 1
}

show()
</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 23 '06 #2
Evertjan. wrote:

[snip]
"window" is the default and top element of the DOM tree.
In browsers, the window identifier is a reference to the global object.
The root of the document tree is the document object.

[snip]
"this" is the top element of the local DOM scope,
and that could be a <div>, an <a>, etc.
The this operator value is determined when entering an execution context
(normally, that means calling a function).

When executing "global" code:

<script type="text/javascript">
/* "global" code */
</script>

the this operator value is a reference to the global object.

this == window; // true

When calling a function as a method of some other object, the this
operator refers to that object:

var object = {
method : function() {
return this;
}
};

object.method() == object; // true

However, when a function is called directly, the this operator once
again refers to the global object:

var func = object.method;

func() == object; // false
func() == window; // true

Typically, event listeners are called by the browser as if they were a
method of the element. As a result, the this operator will refer to that
element. An exception is if the listener was added using MSIE's
attachEvent method; the this operator will refer to the global object.
>Also, is the "self" object the same as the "document" or "this" object?

"self" seems to be used outside the browser and DOM environment, ...
Perhaps, but it is also a property of the window (global) object, and is
a reference to the current window or frame.

[snip]

Mike
Sep 23 '06 #3

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

Similar topics

3
by: Phil | last post by:
Hi everybody, I am a XSLT beginner and the following problem really makes me crazy ! I have a main "contacts.xml" document which contains references to several contact data XML files. My aim...
1
by: Christoph | last post by:
newFieldElement = document.createElement( 'INPUT' ); newFieldElement.onblur = new Function( "calculatePremiumOptionTotal( this );" ); In my (dynamically generated) javascript, I've never had a...
8
by: Just Me | last post by:
1)How can I find the folders like "My Recent Document", "Desktop", "My Pictures",... if I want to store a file there? 2) How can I find the icon for those files if I want to display it? ...
9
by: Ant | last post by:
Hi, I'm wondering if anybody can tell me what the Validate document button is used for & how to use it. When I deliberately put some tag in that shouldn't be there, (such as an extra body tag) &...
1
by: gslim | last post by:
I am trying to implement the busybox sample from // From Mark Wagner // http://blogs.crsw.com/mark/articles/642.aspx When I get to this line I get an access denied error. Could someone give me...
2
by: danny.dion | last post by:
Hi ! I have a question about JScript : I have an object class wich dynamically creates a control in the page. Then it binds an event to that control, pointing on one of its methods (the...
0
by: =?Utf-8?B?QXR1bA==?= | last post by:
When .Net 1.0 webservice (VS2003) generates a wsdl - <wsdl:binding name="TestSoap" type="tns:TestSoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/(note:...
5
by: Jeremy | last post by:
Is there any good reading about pitfalls of scoping when using events? Here is my specific issue: function MyType() { this.foo = "bar"; this.textbox = document.createElement("input");...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.