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

Overriding native HTMLElement.addEventListener

P: n/a
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
Jul 23 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Jul 23 '08 #2

P: n/a
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 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
Jul 24 '08 #3

P: n/a
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 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
Jul 24 '08 #4

P: n/a
* 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.
Jul 30 '08 #5

P: n/a
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.
Jul 30 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.