473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Extending Node w/ addEventListener

PJ
Is it possible to extend the Node object so that the DOM function
addEventListener can be used w/ IE? Does anyone have an example of this?

Thanks,
Paul
Jul 29 '05 #1
4 2919


PJ wrote:
Is it possible to extend the Node object so that the DOM function
addEventListener can be used w/ IE? Does anyone have an example of this?


Neither MSXML (for the XML DOM) nor IE (for the HTML DOM) expose a Node
function or its prototype to script so if you are thinking about doing
some stuff like
Node.prototype.method = function () { ... };
then no, that is not possible with MSIE.
But some extension of the HTML DOM in IE/Win is possible using
behaviors, that way you can implement some properties or methods and
bind them to all or some elements in the page using a CSS selector:
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/behaviors/howto/creating.asp>

As for addEventListener, IE 5.5/6 have attachEvent which at least allows
setting multiple event handlers for an element.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 29 '05 #2
i'm starting to see that now...i apologize in advance for my ignorance w/
this language....

have you found a better solution for event handling then a library such as
this?

http://www.developer-x.com/behaviour/listener.js
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:42***********************@newsread4.arcor-online.net...


PJ wrote:
Is it possible to extend the Node object so that the DOM function
addEventListener can be used w/ IE? Does anyone have an example of this?


Neither MSXML (for the XML DOM) nor IE (for the HTML DOM) expose a Node
function or its prototype to script so if you are thinking about doing
some stuff like
Node.prototype.method = function () { ... };
then no, that is not possible with MSIE.
But some extension of the HTML DOM in IE/Win is possible using behaviors,
that way you can implement some properties or methods and bind them to all
or some elements in the page using a CSS selector:
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/behaviors/howto/creating.asp>

As for addEventListener, IE 5.5/6 have attachEvent which at least allows
setting multiple event handlers for an element.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 29 '05 #3
btw...yes, my hope was something like...

if ( !window.addEventListener && window.attachEvent )
Node.prototype.addEventListener = function(element, func) {
window.attachEvent(element, func);
}
but that's still probably wrong syntax and i'd appreciate correction. thx
guys. ( should it be return window.attachEvent, ect. )

TIA and forever,
PJ

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:42***********************@newsread4.arcor-online.net...


PJ wrote:
Is it possible to extend the Node object so that the DOM function
addEventListener can be used w/ IE? Does anyone have an example of this?


Neither MSXML (for the XML DOM) nor IE (for the HTML DOM) expose a Node
function or its prototype to script so if you are thinking about doing
some stuff like
Node.prototype.method = function () { ... };
then no, that is not possible with MSIE.
But some extension of the HTML DOM in IE/Win is possible using behaviors,
that way you can implement some properties or methods and bind them to all
or some elements in the page using a CSS selector:
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/behaviors/howto/creating.asp>

As for addEventListener, IE 5.5/6 have attachEvent which at least allows
setting multiple event handlers for an element.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 29 '05 #4
VK
Paul Walker wrote:
btw...yes, my hope was something like...

if ( !window.addEventListener && window.attachEvent )
Node.prototype.addEventListener = function(element, func) {
window.attachEvent(element, func);
}
but that's still probably wrong syntax and i'd appreciate correction. thx
guys. ( should it be return window.attachEvent, ect. )

There are more problems than simply make a copy of a function under
another name.
1) addEventListener requires three arguments: event name, function
reference and capturing phase
attachEvent has only two arguments: event name and function
reference.
2) addEventListener wants all event names in "pure" form (w/o "on"):
"click", "load".
attachEvent wants all event names in "on" form: "onclick", "onload".

As it was explained before you cannot extend the prototype of every
single HTML element at once any way.

I'm using sometimes (when events are getting to messy) this self-made
event dispatcher. The other benefit is that it will normalise event
names so can use any form you prefer (with "on" or w/o "on"):
function EventDispatcher() {
this.getClassName = function() {return 'EventDispatcher';}
this.addListener = function(obj, evt, fun, b) {
var result = true;
var eventPhase = (typeof(b)=='boolean') ? b : false;
var eventName = normaliseEventName(evt,obj);
if ('addEventListener' in obj) {
obj.addEventListener(eventName, fun, eventPhase);
}
else if ('attachEvent' in obj) {
obj.attachEvent(eventName, fun);
}
else {
result = false;
}
return result;
}

function normaliseEventName(eName, context) {
var rightName = eName.toLowerCase();
if (('addEventListener' in context)&&
(rightName.charAt(0) == 'o')) {
rightName = rightName.substring(2);
}
else if (('attachEvent' in context)&&
(rightName.charAt(0) != 'o')) {
rightName = 'on' + rightName;
}
else {
/*NOP*/
}
return rightName;
}
}
and then later:
var Dispatcher = new EventDispatcher();
Dispatcher.addListener(obj,'onclick',foo);
// or Dispatcher.addListener(obj,'click',foo);
If you find it anyhow useful, you're welcome to extend the prototype
with
this.removeListener

Jul 29 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Patient Guy | last post by:
I have this in a document: <body onload="a_function(this);"> In an (external) script file, I define a_function() as follows: function a_function(obj) { // surprise errant coding here
1
by: Colin Green | last post by:
OK here's is what I wish to do. I have an XML file that I want to read into an XmlDocument. I then want to be able to interrogate the XmlNodes to find both their start AND end character positions...
2
by: Michael C | last post by:
Question about extending a class. I extended the TreeNode class, like this: class ExtendedTreeNode : TreeNode { public int value1; public int value2; public int NodeLevel() { //This...
1
by: Joel Byrd | last post by:
I am having a problem adding a node to the page. This works on other browser (Opera, Firefox, etc.), but not in IE. The following is the function to add the node: (the id "test_div" is a...
3
by: Jake Barnes | last post by:
37 Signals has built some awesome software with some features I wish I knew how to imitate. When I'm logged into my page (http://lkrubner.backpackit.com/pub/337271) any item that I mouseOver I'm...
5
by: Don | last post by:
I've created a small test class to extend the Treenode object and am having mixed success. In the Treeview's 'BeforeExpand' event I've used code from the help topic "Adding Custom Information to...
5
by: Logos | last post by:
I'm having trouble with this in IE; annoyingly it works beautifully in FF. My keywords are not specific enough to narrow my search to useful entries on google, way too many hits, so here I am...
4
by: chotiwallah | last post by:
i'm having a problem with ie's node object. this is a code snippet: $element = document.createElement('button'); $element.setAttribute('onclick', 'some_function();'); the button element is...
2
by: Daz | last post by:
Hi. I would like to know how to obtain a reference to (or at least, element type of) the last node which was inserted into the document. I am using an event listener to listen for dom inserts,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.