IE's Dummy attachEvent?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hifchan@yahoo.com

    IE's Dummy attachEvent?

    I'd like to attach an event to a group of possible controls, each
    event's handling function might exepect different parameters. By
    learning IE's attachEvent, I found no way to pass parameters.

    My situation is I'm using XML to dynamically generate the event
    function, inside of XML data there is a attribute specify what
    associates with a particular control and parameter(s). Doing things
    like this,

    objElement.atta chEvent("onclic k", eventFunc)

    I'm completely out of wood.

    There are some discussions suggesting to detect srcElement, but that's
    AFTER the function is created.

  • Alexis Nikichine

    #2
    Re: IE's Dummy attachEvent?

    hifchan@yahoo.c om wrote:[color=blue]
    > I'd like to attach an event to a group of possible controls, each
    > event's handling function might exepect different parameters. By
    > learning IE's attachEvent, I found no way to pass parameters.
    >
    > My situation is I'm using XML to dynamically generate the event
    > function, inside of XML data there is a attribute specify what
    > associates with a particular control and parameter(s). Doing things
    > like this,
    >
    > objElement.atta chEvent("onclic k", eventFunc)
    >
    > I'm completely out of wood.
    >
    > There are some discussions suggesting to detect srcElement, but that's
    > AFTER the function is created.
    >[/color]

    Try this:

    function makeEventFunc( param1, param2 )
    {
    return function()
    {
    alert( "param1 = " + param1 + "\nparam2 = " + param2 );
    }
    }

    objElement.atta chEvent( "onclick", makeEventFunc( param1, param2 ) );

    That way, makeEventFun will return you a closure, i.e. a function who
    "remembers" what values were bound to param1 and param2. This "function"
    can then be passed to attachEvent.

    Hope this helps,

    Alexis

    --
    Some domain is free

    Comment

    • Daniel Kirsch

      #3
      Re: IE's Dummy attachEvent?

      Alexis Nikichine schrieb:[color=blue]
      > Try this:
      >
      > function makeEventFunc( param1, param2 )
      > {
      > return function()
      > {
      > alert( "param1 = " + param1 + "\nparam2 = " + param2 );
      > }
      > }
      >
      > objElement.atta chEvent( "onclick", makeEventFunc( param1, param2 ) );[/color]

      Nice trick!

      Comment

      Working...