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

Setting custom properties

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;
Jul 23 '05 #1
5 2160
On 30 Aug 2004 14:46:48 -0700, Jason Butera <jj******@hotmail.com> wrote:
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:

<URL:http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html#getEl>
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3
Jason Butera wrote:
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;
}
}

<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/obj.html#1018325>
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
--
God punishes those who survive too extravagantly.
Jul 23 '05 #4
Thomas 'PointedEars' Lahn wrote:
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
Jul 23 '05 #5
> 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/a...ctions/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/a...s/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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #6

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

Similar topics

8
by: JamesBV | last post by:
My PC's Region setting is set to "English (Canada)"... cause I am in Canada (eh?) (: I'm using VB.net, standard edition (v1?) So I wrote my application based on this Region. I've both Short...
1
by: rforman1 | last post by:
I have found many threads with this question asked and ostensibly answered, but none with an example of how exactly to do it. Help! I know that in order to set up the values of command line...
6
by: Angel | last post by:
I have a button, combo, and custom control that i created. When I click the button i want to set certain display properties of my custom control depending whats in the combo. I set those properties...
1
by: Angel | last post by:
I have a button, combo, and custom control that i created. When I click the button i want to set certain display properties of my custom control depending whats in the combo. I set those properties...
8
by: David Lozzi | last post by:
Howdy, I have a user control that is a report to display data. On the page the control is inserted in, I have filter options to filter the report. When I try to do something like this, nothing...
0
by: Bishop | last post by:
I've figured out how to create a custom TreeNode with custom properties and I can view those properties at runtime, but I'm not sure how to change them after I've added the custom node to the tree....
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
5
by: jegec | last post by:
Hi all! Brief subject: I have to develop an ASP-based application, and build also a specific error handling ASP. After that I had set the virtual directory Custom Error 500;100 to new ASP -...
3
by: William Youngman | last post by:
I am on a team that is developing a proposal generation web application and we are using a custom base page (ProGenBase.cs) located in the app_code directory and all of the app's web pages inherit...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.