Adding methods to Node or Element objects?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Weston C

    Adding methods to Node or Element objects?

    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



    ~==~

    Taking Pictures During Dreams
    weston8[at]cann8central.or g
    (remove eights to email me)

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Lasse Reichstein Nielsen

    #2
    Re: Adding methods to Node or Element objects?

    Weston C <west8on[at]cann8central.Re moveEights.org> writes:
    [color=blue]
    > 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:[/color]

    Not generally.
    In Mozilla, you can use
    Node.prototype. myNewMethod = function (...){...};
    Other browsers don't have the constructor function for nodes available.
    [color=blue]
    > Javascript/ECMAscript, iirc, however, uses a prototype
    > object-oriented model that doesn't have interfaces, right?[/color]

    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 - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Grant Wagner

      #3
      Re: Adding methods to Node or Element objects?

      Weston C wrote:
      [color=blue]
      > 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[/color]

      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="docume nt.getElementBy Id('blah').myId ();">blah</button>
      <button onclick="docume nt.getElementBy Id('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.getEle mentsByTagName( 'div');
      if (divs != null) {
      for (var i = 0; i < divs.length; i++) {
      divs[i].myId = myId;
      }
      }
      function myId() {
      alert(this.id);
      }
      </script>
      <button onclick="docume nt.getElementBy Id('blah').myId ();">blah</button>
      <button onclick="docume nt.getElementBy Id('bleh').myId ();">bleh</button>

      --
      | Grant Wagner <gwagner@agrico reunited.com>

      * Client-side Javascript and Netscape 4 DOM Reference available at:
      *


      * Internet Explorer DOM Reference available at:
      *
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


      * 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


      Comment

      Working...