User Name Password Register
DaniWeb IT Discussion Community
All
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
Nov 11th, 2006
Views: 6,353
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 ('/*' != '//')
Last edited : Feb 14th, 2007.
javascript Syntax
  1. /* Start of the multihandle Object...*/
  2. function MultiHandle(owner){
  3. var my_handlers = new Array();
  4. var my_owner = owner;
  5. this.append = function(handler){
  6. my_handlers[my_handlers.length] = handler;
  7. }
  8. this.fire = function(evt){
  9. var i;
  10. for(i = 0; i < my_handlers.length; i++){
  11. my_owner.tempspace = my_handlers[i];
  12. my_owner.tempspace(evt);
  13. }
  14. }
  15. }
  16. /* End of the multihandle object*/
  17.  
  18. /* start of the object add event handler script */
  19.  
  20.  
  21. /*This bit goes where you'd normally write...
  22. ... object.onmouseup = [event handler]...
  23. ... where [event handler] is an existing function ...
  24. ... that handles an event, or even an
  25. ... anonymous function.*/
  26.  
  27. if(typeof(MultiHandle) != "undefined"){
  28. var mup_handler = object.mh_onmouseup;
  29. if(!mup_handler){
  30. mup_handler = new MultiHandle(object);
  31. object.mh_onmouseup = mup_handler;
  32. object.onmouseup = function(evt){this.mh_onmouseup.fire(evt);};
  33. }
  34. mup_handler.append([event handler]);
  35. }else{
  36. object.onmouseup = [event handler];
  37. }
  38.  
  39. /* end of the object add event handler script*/
Comments (Newest First)
MattEvans | Posting Shark | Jan 21st, 2007
When I wrote that code; it was after reading that there was no standardised method to do such a thing.. even then perhaps that was an outdated fact.

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.
digital-ether | Practically a Master Poster | Jan 15th, 2007
lil typo there..

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:

/**
* 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
The append handler part can be simplified to a single public function as so:

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;
  }
}

and then called like:

multihandle_append(document.getElementById('object'),"onmouseup",function(param){code});
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 6:34 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC