472,958 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1498
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.