Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overriding native HTMLElement.addEventListener

7 views
Skip to first unread message

marchaos

unread,
Jul 23, 2008, 12:01:52 PM7/23/08
to
I'm trying to override the native HTMLElement.addEventListener in
firefox so that I can do some extra things. I know it's possible to
add methods to the HTMLElement prototype and for them to be present on
instances of that element, but modifying native methods doesn't seem
to stick ie:

HTMLElement.prototype.addEventListener = function()
{
alert('overridden');
};

HTMLElement.prototype.anotherMethod= function()
{
alert('anothermethod');
};

var div = document.createElement('div');
alert(div.anotherMethod.toString()); // alerts function ()
{ alert('anotherMethod'); }
alert(div.addEventListener.toString()); // alerts function() { [native
code ]};

Does anyone know if its possible to override a native method in
forefox ?

Marc

Thomas 'PointedEars' Lahn

unread,
Jul 23, 2008, 2:15:06 PM7/23/08
to
marchaos wrote:
> Does anyone know if its possible to override a native method in
> [Firefox]?

It is possible, by direct assignment, to ignore the existing method of a
host object (you cannot access objects directly, you can only use references
to them) but it is error-prone always. Forget about it and use a wrapper
method (maybe a property of a user-defined wrapper object) instead.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

marchaos

unread,
Jul 24, 2008, 4:33:43 AM7/24/08
to
Cheers.

I guess I will have to resort to something that Prototype does and
have an "observe"
method that wraps addEventListener. I was hoping to keep the W3C API
though :(

On Jul 23, 7:15 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:

marchaos

unread,
Jul 24, 2008, 4:34:00 AM7/24/08
to
Cheers.

I guess I will have to resort to something that Prototype does and
have an "observe"
method that wraps addEventListener. I was hoping to keep the W3C API
though :(

On Jul 23, 7:15 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:

Bjoern Hoehrmann

unread,
Jul 30, 2008, 7:35:28 AM7/30/08
to
* marchaos wrote in comp.lang.javascript:

>I'm trying to override the native HTMLElement.addEventListener in
>firefox so that I can do some extra things. I know it's possible to
>add methods to the HTMLElement prototype and for them to be present on
>instances of that element, but modifying native methods doesn't seem
>to stick ie:

Note that .addEventListener() is not on HTMLElement but on EventTarget
(a different interface that is implemented by all objects that implement
the Element interface). Whether that helps you with your problem I do
not know, I would rather second Thomas' advice.

Henry

unread,
Jul 30, 2008, 8:28:02 AM7/30/08
to
On Jul 30, 12:35 pm, Bjoern Hoehrmann wrote:
> * marchaos wrote in comp.lang.javascript:
<snip>

> Note that .addEventListener() is not on HTMLElement but on
> EventTarget (a different interface that is implemented by
> all objects that implement the Element interface). ...

The DOM Level 2 Events specification says "The EventTarget interface
is implemented by all Nodes in an implementation which supports the
DOM Event Model". So it is objects implementing the Node interface
that should implement the EventTarget.

0 new messages