P
Pavils Jurjans
Hallo,
I have been programming for restricted environments where Internet Explorer
is a standard, so I haven't stumbled upon this problem until now, when I
need to write a DOM-compatible code.
The question is about best practices for passing parameters to an event
function.
I have, say, the following HTML:
<div onclick="myEventHandler()">Here's some text</div>
and here's the event handler function:
function myEventHandler(evt) {
evt = (evt) ? evt : ((event) ? event : null); // to get single interface
to the IE event object or the DOM event object.
// Now must handle the event
}
In the example case, the event handler is written directly in the html code,
or the strings is outputed with document.write or element.innerHTML is
assigned. However, in more general way, my application framework allows also
to snap that event to most any tag and still the handling should be exatly
as defined. There are no multiple handler functions, the behaviour is
determined by the function that took care for attaching the event. That
means, I *have* to pass some parameters with the handler function call. In
IE that ie easy, as I have no requirement to provide a single argument to
the handler function that is filled with event object. I can freely write
onclick="myHandler(123)" and it works fine. Now, I see some approaches to
handle this parameter passing problem. One is assigning the parameters to
some global variable and then accessed from within the event handler
function:
onclick="parms={a:1, b:2}; myHandler()"
function myHandler() {
alert(parms.a + "; " + parms.b);
}
it's ugly, since I dont like that function refers to external global
variable parm, but it works. However I am not sure if the values of
parameters are persistent while the event handler and all the function call
chain is processed.
So, maybe there is some standard approach practised among ECMAscript
developers?
Thanks,
-Pavils Jurjans
I have been programming for restricted environments where Internet Explorer
is a standard, so I haven't stumbled upon this problem until now, when I
need to write a DOM-compatible code.
The question is about best practices for passing parameters to an event
function.
I have, say, the following HTML:
<div onclick="myEventHandler()">Here's some text</div>
and here's the event handler function:
function myEventHandler(evt) {
evt = (evt) ? evt : ((event) ? event : null); // to get single interface
to the IE event object or the DOM event object.
// Now must handle the event
}
In the example case, the event handler is written directly in the html code,
or the strings is outputed with document.write or element.innerHTML is
assigned. However, in more general way, my application framework allows also
to snap that event to most any tag and still the handling should be exatly
as defined. There are no multiple handler functions, the behaviour is
determined by the function that took care for attaching the event. That
means, I *have* to pass some parameters with the handler function call. In
IE that ie easy, as I have no requirement to provide a single argument to
the handler function that is filled with event object. I can freely write
onclick="myHandler(123)" and it works fine. Now, I see some approaches to
handle this parameter passing problem. One is assigning the parameters to
some global variable and then accessed from within the event handler
function:
onclick="parms={a:1, b:2}; myHandler()"
function myHandler() {
alert(parms.a + "; " + parms.b);
}
it's ugly, since I dont like that function refers to external global
variable parm, but it works. However I am not sure if the values of
parameters are persistent while the event handler and all the function call
chain is processed.
So, maybe there is some standard approach practised among ECMAscript
developers?
Thanks,
-Pavils Jurjans