•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 425,880 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,203 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
You want your object to do a number of different things when it's clicked, and you've used up onmouseup, onmousedown and onclick. There are some properietry solutions to this, but it's quite a simple thing really.
Copy and paste the MultiHandle object into an empty js file, include it in your page, and wherever you'd write an event handler, use the MultiHandle object event handler script in the listing.
The standard behaviour is to fire each function in the order in which they were appended to the MultiHandle object. I bind the Multihandle for each event to the object that uses it somewhere, following a simple naming convention for which one is for where. Then I don't have to set everything up at the same time, and I never accidently overwrite a handler.
You could create a feedback chain in the MultiHandle fire event so that the next event only occurs if the last event returned true (or didn't return false). It's not the behaviour I wanted when I needed multiple handlers though.
Let me know if it doesn't work in a context, I made it for something very specific and it works there
Edited this (14th Feb 2007) because the snippet was one giant JS comment ('/*' != '//')
Copy and paste the MultiHandle object into an empty js file, include it in your page, and wherever you'd write an event handler, use the MultiHandle object event handler script in the listing.
The standard behaviour is to fire each function in the order in which they were appended to the MultiHandle object. I bind the Multihandle for each event to the object that uses it somewhere, following a simple naming convention for which one is for where. Then I don't have to set everything up at the same time, and I never accidently overwrite a handler.
You could create a feedback chain in the MultiHandle fire event so that the next event only occurs if the last event returned true (or didn't return false). It's not the behaviour I wanted when I needed multiple handlers though.
Let me know if it doesn't work in a context, I made it for something very specific and it works there

Edited this (14th Feb 2007) because the snippet was one giant JS comment ('/*' != '//')
Last edited : Feb 14th, 2007.
/* Start of the multihandle Object...*/ function MultiHandle(owner){ var my_handlers = new Array(); var my_owner = owner; this.append = function(handler){ my_handlers[my_handlers.length] = handler; } this.fire = function(evt){ var i; for(i = 0; i < my_handlers.length; i++){ my_owner.tempspace = my_handlers[i]; my_owner.tempspace(evt); } } } /* End of the multihandle object*/ /* start of the object add event handler script */ /*This bit goes where you'd normally write... ... object.onmouseup = [event handler]... ... where [event handler] is an existing function ... ... that handles an event, or even an ... anonymous function.*/ if(typeof(MultiHandle) != "undefined"){ var mup_handler = object.mh_onmouseup; if(!mup_handler){ mup_handler = new MultiHandle(object); object.mh_onmouseup = mup_handler; object.onmouseup = function(evt){this.mh_onmouseup.fire(evt);}; } mup_handler.append([event handler]); }else{ object.onmouseup = [event handler]; } /* end of the object add event handler script*/
Comments (Newest First)
MattEvans | Posting Shark | Jan 21st, 2007
digital-ether | Practically a Master Poster | Jan 15th, 2007
•
•
•
•
lil typo there..
should be:
note the: 'on'+evType
should be:
/**
* Add events
* @param Object HTML Element
* @param string name of trigger
* @param string name of function to attach
* @param bool capture
*/
addEvent = function(el, evType, fn, useCapture) {
if (el.addEventListener) {
el.addEventListener(evType, fn, useCapture);
return true;
}
else if (el.attachEvent) {
var r = el.attachEvent('on' + evType, fn);
return r;
}
else {
multihandle_append(el, 'on'+evType, fn);
}
}note the: 'on'+evType
digital-ether | Practically a Master Poster | Jan 15th, 2007
•
•
•
•
The DOM 2 Specs implements addEventListener().
IE uses attachEvent().
Thus you can have something like:
maybe your code could be used together with this to attach multiple events if the browser does not support addEventListener and attachEvent.
now we can use the DOM implementation or IE if supported, but fall back to multihandle if not..
IE uses attachEvent().
Thus you can have something like:
/**
* Add events
* @param Object HTML Element
* @param string name of trigger
* @param string name of function to attach
* @param bool capture
*/
addEvent = function(el, evType, fn, useCapture) {
if (el.addEventListener) {
el.addEventListener(evType, fn, useCapture);
return true;
}
else if (el.attachEvent) {
var r = el.attachEvent('on' + evType, fn);
return r;
}
else {
el['on' + evType] = fn;
}
}maybe your code could be used together with this to attach multiple events if the browser does not support addEventListener and attachEvent.
// .. include the MultiHandle Object of course
function multihandle_append(object,event,func){
if(typeof(MultiHandle) != "undefined"){
var mup_handler = object["mh_"+event];
if(!mup_handler){
mup_handler = new MultiHandle(object);
object["mh_"+event] = mup_handler;
object[event] = function(evt){this["mh_"+event].fire(evt);};
}
mup_handler.append(func);
}else{
object.onmouseup = func;
}
}
/**
* Add events
* @param Object HTML Element
* @param string name of trigger
* @param string name of function to attach
* @param bool capture
*/
addEvent = function(el, evType, fn, useCapture) {
if (el.addEventListener) {
el.addEventListener(evType, fn, useCapture);
return true;
}
else if (el.attachEvent) {
var r = el.attachEvent('on' + evType, fn);
return r;
}
else {
multihandle_append(el, evType, fn);
}
}now we can use the DOM implementation or IE if supported, but fall back to multihandle if not..
MattEvans | Posting Shark | Jan 3rd, 2007
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)
The main problem with multihandle_append is it will always overwrite handlers if there is no MultiHandle object yet for that event.,. So you have to assign all event handlers using javascript code (as apposed to just typing onclick = "etc.."). The multihandle_append function could probably be hacked a bit more to add any existing event handlers to the top of a new MultiHandle object's function s(t)ack..
I imagine the new DOM spec method respects attributed event handlers automatically; I'd definately advise using that availability checking code from digital-ether; and using MH as a dependable last-resort rather than an optimum first choice.