Setting custom properties

J

Jason Butera

I know that I can read/write custom properties of an object by using
the following:

Setting:
document.all['Control'].customProp = "this";
Getting:
document.all['Control'].customProp;

Is there a way I can run code when this custom property is set. Or
perhaps there is a way to create a custom method?

Example:
<span id="MySpan"><input type="text"></span>

I'd the property:
document.all['MySpan'].enabled = true;
To automatically do this:
document.all['MySpan'].controls[0].disabled = !thevalue;

OR the method
document.all['MySpan'].enable();
To do this:
document.all['MySpan'].disabled = false;
 
M

Michael Winter

I know that I can read/write custom properties of an object by using
the following:

Setting:
document.all['Control'].customProp = "this";
Getting:
document.all['Control'].customProp;

If you plan for this code to run on any browser other than IE (and its
compatibles), don't use document.all. See:

Is there a way I can run code when this custom property is set.

There are extensions that allow it, but it seems that a lot of browsers
don't allow it. It's safer to use methods.
Or perhaps there is a way to create a custom method?

You can attach a method to an object by assigning a function reference to
a property. This reference can either be a standard function statement:

function myFunction() {
// do stuff
}

obj.myMethod = myFunction;
obj.myMethod();

or a function expression:

obj.myMethod = function() {
// do stuff
};
obj.myMethod();

Note that in both cases, only 'obj' has the myMethod property. If this
were a user-defined object, you could use the prototype property of the
constructor to add the method universally.

function MyObject() {
}
MyObject.prototype.myMethod = function() {
};

var obj = new MyObject();
obj.myMethod();

However, support for the prototype object on host objects, such as HTML
elements, is not widely available. It might be easier to define a global
function that takes a reference to the host object in question, and any
other arguments, and work that way.
Example:
<span id="MySpan"><input type="text"></span>

I'd the property:
document.all['MySpan'].enabled = true;
To automatically do this:
document.all['MySpan'].controls[0].disabled = !thevalue;

SPAN elements don't have a controls collection, even in IE.
OR the method
document.all['MySpan'].enable();
To do this:
document.all['MySpan'].disabled = false;

Only IE, as far as I know, supports the disabled property on non-form
elements.

Hope that helps,
Mike
 
J

Jason Butera

Thanks a lot Michael.

1. My app is internal for IE only so document.all is OK. I'll keep your
tip in mind for my other apps.

2. I don't need to prototype. I just need to set individual objects.

3. You are correct, span does not have a controls collection. I meant to
say "children".

I appreciate your help. I'll get to work on it!

Jason
 
T

Thomas 'PointedEars' Lahn

Jason said:
I know that I can read/write custom properties of an object by using
the following:

Setting:
document.all['Control'].customProp = "this";
Getting:
document.all['Control'].customProp;

You _don't_ know.

1. document.all is a feature of the IE browser component (IE).

2. The MSDN Library specifies document.all to be a method, not
an object. There is an ambiguity in the IE DOM to allow
collections be referenced like methods and vice-versa, but
I recommend to stick to the documentation:

document.all('Control').customProp = "this";

3. You don't test for your references prior to access:
<http://www.pointedears.de/scripts/test/whatami>

4. ECMAScript (3) allows host objects be specified in a
way that it is impossible to add properties to them.
Is there a way I can run code when this custom property is set.

You mean a setter, available in JavaScript. But IE
supports JScript and so you cannot do that there.
Or perhaps there is a way to create a custom method?

A method is but a property of type "function" (and sometimes in the IE DOM,
for host objects, of type "object"), so if you can add properties to an
object, you can add methods as well.
Example:
<span id="MySpan"><input type="text"></span>

What should this achieve?
I'd the property:
document.all['MySpan'].enabled = true;
To automatically do this:
document.all['MySpan'].controls[0].disabled = !thevalue;

Not in JScript. In JavaScript, in a Gecko-based
browser you could do this (quick hack):

var o;
if (document
&& document.getElementById
&& (o = document.getElementById('MySpan')))
{
o.enabled setter = function(v)
{
if (typeof this.controls == "undefined")
{
this.controls = [];
}

if (typeof this.controls[0] == "undefined")
{
this.controls[0] = {};
}

this.controls[0].disabled = !v;
}
}

OR the method
document.all['MySpan'].enable();
To do this:
document.all['MySpan'].disabled = false;

This is possible with every ECMAScript compliant implementation,
including JScript, provided that the host object (o) exists and
supports adding properties (see above):

// ...
o.enable = function()
{
this.disabled = false;
}
// ...

In an ECMAScript 3 compliant implementation, like JScript 5.6,
one should do

// ...
try
{
document.all('MySpan'].enable = function()
{
this.disabled = false;
}
}
catch (e)
{
// handle the exception here
}
// ...


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
In an ECMAScript 3 compliant implementation, like JScript 5.6,
one should do

// ...
try
{
document.all('MySpan'].enable = function()

Should be

o.enable = function()
{
this.disabled = false;
}
}
catch (e)
{
// handle the exception here
}
// ...


PointedEars
 
G

Grant Wagner

2. The MSDN Library specifies document.all to be a method, not
an object. There is an ambiguity in the IE DOM to allow
collections be referenced like methods and vice-versa, but
I recommend to stick to the documentation:

document.all('Control').customProp = "this";

<url: http://msdn.microsoft.com/workshop/author/dhtml/reference/collections/all.asp />

"all Collection"
....
"Remarks - The all collection includes..."
....
"Standards Information - There is no public standard that applies to this collection."


The MSDN Library documentation clearly does not specify document.all to be a method. The
MSDN Library documentation may use "method-like" syntax for collections, but collections are
clearly documented to be collections, not methods.


To add further weight to the argument that [] are the correct notation to use for IE
collections:

<url: http://msdn.microsoft.com/workshop/author/dhtml/reference/collections/elements.asp />

indicates the use of FORM.elements(...), yet I doubt the person proposing the use of
document.all() would recommend the use of FORM.elements(), despite their advice to "stick to
the documentation". Continue using document.all[] since it is supported now, and will
continue to be supported long into the future.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top