Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Function.prototype.toEvent (alias bindAsEventListener) (See related posts)

This one keeps this and sends event object as the first argument (no more checking!). It also has only one parameter or parameters maybe- the argument/s you want to send to your function. Thats it! Bonus: the last argument is the co-ordinates of the event.

// Function.prototype.toEvent, .bindAsEventListener( args ) REM2006 for this.js Api  
// Important: sends event as the first argument to your function in IE also!, 
// event XY [Array][x,y] is passed as last arg, your passed args are in the middle :)
// example: myElement.onclick= myFunc.toEvent( myArguments ); http://richard-m.blogspot.com/
//  
Function.prototype.toEvent = Function.prototype.bindAsEventListener= function(){
  var _method= this, r=[], len=arguments.length, i=0;//alert(len)
  for (i;i<len;i++){ r[i+1]=arguments[i] }
  return function( ev ){  r[0]= ev= ev || event; 
    var db=document.body, dd=document.documentElement;
    r[r.length++]=(ev.pageX)? [ev.pageX, ev.pageY] : 
      [ev.clientX + db.scrollLeft+ dd.scrollLeft, 
       ev.clientY + db.scrollTop + dd.scrollTop];
    return _method.apply(ev.target|| ev.srcElement, r);
  };
};

You need to create an account or log in to post comments to this site.


Related Posts