Overriding native HTMLElement.addEventListener

M

marchaos

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
 
T

Thomas 'PointedEars' Lahn

marchaos said:
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
 
M

marchaos

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 :(

marchaos said:
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
 
M

marchaos

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 :(

marchaos said:
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
 
B

Bjoern Hoehrmann

* 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.
 
H

Henry

* marchaos wrote in comp.lang.javascript:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top