All pastes #364580 Raw Edit

Anonymous

public text v1 · immutable
#364580 ·published 2007-02-20 07:26 UTC
rendered paste body
function bindEvent(n, evt, func)
{
	var name = "my"+ evt;
	
	if ( typeof n[name] == "undefined" )
		n[name] = [];
	
	n[name].push( func );
	
	if ( isDOM )
		return( n.addEventListener(evt, func, false) );
	else
		return( n.attachEvent("on"+ evt, func) );
}

function bindMyEvents(evt, src, targ)
{
	/*
		when you call 'bindEvent' it remembers the function you are binding under the event you are binding it to 
		and associates it to the object.
		
		for example:
			bindEvent(document.body, "mousemove", my_mousemove);
			bindEvent(document.body, "mousemove", my_other_mousemove);
			
		document.body.my_mousemove[0..1] is now an array and holds all the mousemove functions
		
		this function, bindMyEvents, binds all of the 'src' node 'evt' functions to the given target node. for example,
		if I did the following:
		
			bindMyEvents(document.getElementById("my_div"), document.body, "mousedown");
		
		'my_div' would have the same mousedown events as document.body
	*/
	
	var name = "my"+ evt;
		
	if ( typeof src[name] != "undefined" )
	{
		var i, len = src[name].length;
		
		for (i = 0; i < len; i++)
			bindEvent(targ, evt, src[name][i]);
	}
}