Bonzo said:
Duncan & Michael,
Thanks for the input... I will definitely check this option out...
I understand your concern over the design Michael, it is a rather
strange one (my design that is)
It evolves from the desire to build elements via the DOM, then add
eventHandlers to them. (This has already been done, and works like
magic in all browsers except IE)
In Moz, and Opera, I can use:
someElement.setAttribute('onclick', 'funcB(event, ' + param1 + ', ' +
param2 + ');');
This will create the attribute, register the event, and all is well...
of course, IE will fail horribly... it will set the attribute, but not
register the event.
Have you considered using:
var parm1 = 'Parameter 1';
var parm2 = 'Parameter 2';
someElement.onclick = function(event){
funcB(event, parm1, parm2);
}
// ...
function funcB(e, p1, p2){
var e = e || window.event;
alert(e.type + '\n' + p1 + '\n' + p2);
}
Be careful with closures - they may or may not be what you are after,
they can be avoided if not.
So, option2, was to call addEventListener/attachEvent, to add the event
handling... the only problem being, that I can't pass params (which,
argue as one might, I simply need for the "magic" I'm cultivating...
read: "seriously dynamically dynamic HTML")
addEventListener is handy if you want to add multiple events, or don't
want to stomp on those that might already be there, but the above is
simpler and may suit better.
So, I hacked** together something, that will wrap the "event
registration" call, to register the events in Moz, Opera, IE, etc.
A-N-D, allow me to pass parameters!...
If you post a small demo of what you are trying to do, life may be simpler.
**In the hack, I want to pass the event (default first argument in Moz,
Opera, etc.) to my handler, BUT, also (for simplicity), also pass it
along to IE (cause it is plain anoying to have to retrieve it inside
every single event handler on my page, if I can pass it instead)
You can't (AKAIK).
IE has a different event model, you have to get window.event from the
called function, so each such function has to have at least:
function funcB(e)
{
var e = e || window.event;
}
or some similar approach. The alternative is to call the function with
something like:
funcB(event, window.event);
But then funcB has to be something like:
function funcB(eMoz, eIE)
{
var e = eMoz || eIE;
}
and you're back where you started.
***(there is actually more, but I'm trying to simplify here)
Long Story short, I have the event, and I have the parameters, and thus
I want to call the event handling function, with the event, and all
arguments passed to my "event handler wrapper"... on to my actual event
handler...
Then do as suggested above. An event handler wrapper can be used to
prevent closures, but otherwise shouldn't be necessary.
</end-scary-js-idea>
Alternatively, I've been told I might have some luck, iterating over
the arguments, and building up a "function string" that I can eval()
when complete... I'm not sure if this will solve my problem either, but
it seems the "hackiest" of the ideas I've seen thus far.
Yech, it may not be *the* 'hackiest', but certainly getting there.
PS for anyone that is thinking it, no, I can't just stuff in
foo.innerHTML = ''; Although this will work in IE, it is not where I
want, nor care to go.
You are right not try it - it will likely fail some of the time in all
browsers for some elements and all of the time in others.