i have a event bind function like this(though it is not so robust):
bind$=function(o,evt,fn,cb){
var aE='attachEvent';
var aEL='addEventListener';
if(!o[aE]&&o[aEL]){
return o[aEL](evt,fn,!!cb);
}
return o[aE]('on'+evt,fn);
};
some usage like this:
bind$( /* o */ document, /* evt */ 'click', /* fn */ function(txt)
{alert(txt);} );
but how can I pass a parameter to the bound function "fn" ?
Simply define the "fn" argument as an anonymous function that
specifies inside of it all the special parameters or initialization
that you want.
For instance, if your actual event handler takes both the expected
"evt" parameter along with an additional "foo" parameter, then the
"fn" argument could be this:
function (evt) { myhandler(evt, "bar"); }
may be I didnt display my situation clearly.
my bind function goes :
under firefox it is supposed to be like
document.addEventListener( 'click', function(txt){alert(txt);} ,
false );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
but I found problem when trying to transfer a parameter to the one
argument txt.
On event listener call there are two "persistent" objects available:
1) the bound HTML element referred by "this"
2) the event handler referred by "arguments.callee"
The first one can be used to store element-specific extra info. There
is also Function constructor - this is what I am using most often -
and closures which is an awful way IMO to use for identical event
handlers.
-- Option 1 --
refHtmlElement.
addEventListener('click',
new Function(txt, functionBody),
false);
-- Option 2 --
refHtmlElement.
addEventListener('click',
myFunction,
false);
refHtmlElement.args = {
'txt' : txt
};
and then later
function myFunction(evt) {
var txt = this.args.txt;
// ...
}