By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,569 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,569 IT Pros & Developers. It's quick & easy.

Adding methods to Node or Element objects?

P: n/a
I'm wondering if it's possible to add a method to the Node and/or
Element objects (so they'd subsequently be available to any
node/element). I'd assume you could just do something like:

n.myNewMethod = function myNewMethod;

where n is any Node -- but the fact that Nodes and Elements are
explicitly definied as interfaces rather than actual objects in the DOM
standard makes me wary. Javascript/ECMAscript, iirc, however, uses a
prototype object-oriented model that doesn't have interfaces, right?

Thanks,
Weston

~==~
http://weston.canncentral.org/
Taking Pictures During Dreams
weston8[at]cann8central.org
(remove eights to email me)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Weston C <west8on[at]cann8central.RemoveEights.org> writes:
I'm wondering if it's possible to add a method to the Node and/or
Element objects (so they'd subsequently be available to any
node/element). I'd assume you could just do something like:
Not generally.
In Mozilla, you can use
Node.prototype.myNewMethod = function (...){...};
Other browsers don't have the constructor function for nodes available.
Javascript/ECMAscript, iirc, however, uses a prototype
object-oriented model that doesn't have interfaces, right?


Yes. There is no requirement in the W3C DOM that the ECMAScript binding
makes constructors available. You have the factory methods on the document
object if you need to create elements.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
Weston C wrote:
I'm wondering if it's possible to add a method to the Node and/or
Element objects (so they'd subsequently be available to any
node/element). I'd assume you could just do something like:

n.myNewMethod = function myNewMethod;

where n is any Node -- but the fact that Nodes and Elements are
explicitly definied as interfaces rather than actual objects in the DOM
standard makes me wary. Javascript/ECMAscript, iirc, however, uses a
prototype object-oriented model that doesn't have interfaces, right?

Thanks,
Weston


Sure, the following works in Gecko based browsers (Mozilla Firebird,
Mozilla, Netscape 7.x, Camino):

<div id="blah"></div>
<div id="bleh"></div>
<script type="text/javascript">
HTMLDivElement.prototype.myId = function() {
alert(this.id);
}
</script>
<button onclick="document.getElementById('blah').myId();"> blah</button>
<button onclick="document.getElementById('bleh').myId();"> bleh</button>

I figured out how to this here: <url:
http://www.mozilla.org/docs/dom/mozilla/protodoc.html />

For most other browsers you'll just have to assign the method to all the
elements of that type at run-time:

<div id="blah"></div>
<div id="bleh"></div>
<script type="text/javascript">
var divs = document.getElementsByTagName('div');
if (divs != null) {
for (var i = 0; i < divs.length; i++) {
divs[i].myId = myId;
}
}
function myId() {
alert(this.id);
}
</script>
<button onclick="document.getElementById('blah').myId();"> blah</button>
<button onclick="document.getElementById('bleh').myId();"> bleh</button>

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.