HTMLElement.prototype in Internet Explorer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Keirstead

    HTMLElement.prototype in Internet Explorer

    Is there a way I can add a setter/getter to the HTMLElement prototype
    in internet explorer?

    This, for example, works fine in Mozilla:

    HTMLElement.pro totype.__define Setter__("fooba r", function (sFoobar)
    {
    alert( sFoobar );
    });


    Of course my method I want to add is more complex, the question is is
    there any way at all to do this in IE6?
  • Martin Honnen

    #2
    Re: HTMLElement.pro totype in Internet Explorer



    Jason Keirstead wrote:
    [color=blue]
    > Is there a way I can add a setter/getter to the HTMLElement prototype
    > in internet explorer?
    >
    > This, for example, works fine in Mozilla:
    >
    > HTMLElement.pro totype.__define Setter__("fooba r", function (sFoobar)
    > {
    > alert( sFoobar );
    > });
    >
    >
    > Of course my method I want to add is more complex, the question is is
    > there any way at all to do this in IE6?[/color]

    There is no
    HTMLElement
    exposed in IE's object model, there is no prototype for host objects
    exposed in IE's object model, and the JScript engine doesn't support
    __defineSetter_ _ as that is a JavaScript 1.5 extension to the ECMAScript
    standard.


    --

    Martin Honnen

    Comment

    • Grant Wagner

      #3
      Re: HTMLElement.pro totype in Internet Explorer

      Martin Honnen wrote:
      [color=blue]
      > Jason Keirstead wrote:
      >[color=green]
      > > Is there a way I can add a setter/getter to the HTMLElement prototype
      > > in internet explorer?
      > >
      > > This, for example, works fine in Mozilla:
      > >
      > > HTMLElement.pro totype.__define Setter__("fooba r", function (sFoobar)
      > > {
      > > alert( sFoobar );
      > > });
      > >
      > >
      > > Of course my method I want to add is more complex, the question is is
      > > there any way at all to do this in IE6?[/color]
      >
      > There is no
      > HTMLElement
      > exposed in IE's object model, there is no prototype for host objects
      > exposed in IE's object model, and the JScript engine doesn't support
      > __defineSetter_ _ as that is a JavaScript 1.5 extension to the ECMAScript
      > standard.[/color]

      Although the following is possible:

      <script type="text/javascript">
      var type, elements;
      if (((type = typeof document.getEle mentsByTagName) == 'function' ||
      (type == 'object' && document.getEle mentsByTagName) ) &&
      !!(elements = document.getEle mentsByTagName( '*'))) {
      var i = elements.length ;
      while (i--) {
      elements[i].setFoobar = function(sFooba r) {
      alert(this.node Name + ';' + sFoobar);
      }
      }
      }
      document.body.s etFoobar('this is a test');
      </script>

      Yes, I understand it is not the same as defining a setter for the
      HTMLElement.pro totype, but it does allow you to attach a method to every
      element on the page, and it works in IE6SP1, Gecko-based browsers (Firefox,
      Mozilla, Camino, Netscape 6+) and Opera 7.54. It even works in Opera 6.05,
      although nodeName returns "undefined" so it is of limited usefulness there.

      Of course any new elements added to the page programmaticall y will not have
      the function as part of it's prototype, although you could add the function
      when you construct the element:

      var myDiv = document.create Element("div");
      myDiv.setFoobar = function(sFooba r) {
      alert(this.node Name + ';' + sFoobar);
      }

      That condition would probably be clearer written as:

      var type = typeof document.getEle mentsByTagName;
      if (type == 'function' || (type == 'object' &&
      document.getEle mentsByTagName) {
      var elements = document.getEle mentsByTagName( '*');
      if (elements) {
      // ...
      }
      }

      Lastly. Of course my code can have a dramatic impact on performance on a
      complex page. However, I have my doubts that you would need a setter on
      *every* HTML element on the page, perhaps you would only want it on 3 or 4
      nodeNames. In that case, it would probably be best to retrieve
      document.getEle mentsByTagName( 'div'), 'td', 'a', etc for only the nodeNames
      you actually need the setter attached to.

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      Working...