Common client side EventHandler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Remco

    #1

    Common client side EventHandler

    Hello,

    Serverside I'm generating a html page.
    There are different controls for which I want to create an
    eventhandler manually.

    Like:

    document.forms[0].TextBox1.oncha nge = EventHandler;
    document.forms[0].Button1.onclic k = EventHandler;

    function EventHandler(){
    // do something
    }

    What I would like is to pass arguments to the Eventhandler.

    Like:


    document.forms[0].TextBox1.oncha nge = EventHandler('t est1');
    document.forms[0].Button1.onclic k = EventHandler('t est2');

    function EventHandler(in p){
    // do something conditioned by parameter.
    }


    This sample is simplified, additional, in the EventHandler I'll
    execute the original eventhandlers which I'm gonne store in a
    variable.


    So, the question:

    Is it possible to have an eventhandler with parameters? or should I
    create an eventhandler for all controls which need some extra dynamic
    behavior?

    By the way, Yes, I know I can simply use <input type="text"
    id="TextBox1" onclick="DoFunc tion();" />
    But that's not what I want in this case.


    Thanks in advance!

    Remco
  • Daniel Kirsch

    #2
    Re: Common client side EventHandler

    Remco wrote:[color=blue]
    > Hello,
    >
    > Serverside I'm generating a html page.
    > There are different controls for which I want to create an
    > eventhandler manually.
    >
    > Like:
    >
    > document.forms[0].TextBox1.oncha nge = EventHandler;
    > document.forms[0].Button1.onclic k = EventHandler;
    >
    > function EventHandler(){
    > // do something
    > }
    >
    > What I would like is to pass arguments to the Eventhandler.
    >
    > Like:
    >
    >
    > document.forms[0].TextBox1.oncha nge = EventHandler('t est1');
    > document.forms[0].Button1.onclic k = EventHandler('t est2');[/color]

    You cannot pass additional arguments to your event handler as the call
    will be generated by the browser. However, you may set some additional
    properties for your object and check the event.target for that property.

    eg:

    var elm = document.forms[0].TextBox1;
    elm.param = "foo";
    elm.onchange = EventHandler;

    function EventHandler(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;
    if (target) {
    param = target.param;
    if (param == "foo") {
    // do it
    }
    }
    }

    Daniel

    Comment

    • Michael Winter

      #3
      Re: Common client side EventHandler

      On 2 Dec 2004 03:39:48 -0800, Remco <rembo_r@hotmai l.com> wrote:

      [snip]
      [color=blue]
      > Is it possible to have an eventhandler with parameters? or should I
      > create an eventhandler for all controls which need some extra dynamic
      > behavior?
      >
      > By the way, Yes, I know I can simply use <input type="text"
      > id="TextBox1" onclick="DoFunc tion();" />
      > But that's not what I want in this case.[/color]

      Yes, by emulating what happens when you add a listener via an intrinsic
      event attribute.

      When a user agent (that supports scripting) encounters

      <element onclick="...">

      it creates an anonymous function and inserts your code into it:

      elementRef.oncl ick = function(event) {
      /* ... */
      };

      You can do the exact same thing. Instead of writing

      element.onclick = handler('...');

      use

      element.onclick = function(evt) {
      handler('...');
      };

      Be aware that if 'handler' needs a reference to 'element', you'll have to
      pass it as an argument. Similarly, if you want the event object, you'll
      have to pass that too after changing the above to:

      element.onclick = function(evt) {
      /* IE doesn't pass the event object to
      * listeners - it exists as a global
      */
      evt = evt || event;
      handler('...');
      };

      Hope that helps,
      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Julian Turner

        #4
        Re: Common client side EventHandler

        > Is it possible to have an eventhandler with parameters? or should I[color=blue]
        > create an eventhandler for all controls which need some extra dynamic
        > behavior?[/color]

        The simple answer is not directly.

        When an event handler is called, it can have no parameters. In fact,
        other than IE, an event handler is called with just one parameter -
        the "event" object itself. E.g. for cross-browser event handling:-

        myElement.onlic k=function(eEve nt)
        {
        var eEvent=window.e vent||eEvent;

        // Do something.
        };

        If you want to feed parameters to an event handler then there are two
        basic ways to do this.

        1. Use global level variables. I.e. variables defined at the global
        level of your script.

        <script>
        var myParam=1;


        myElement.onlic k=function(eEve nt)
        {
        var eEvent=window.e vent||eEvent;

        alert(myParam);
        };

        </script>

        2. Add properties to the element itself.

        myElement.aProp erty=1;

        myElement.oncli ck=function()
        {
        function Display(eL)
        {
        alert(eL.aPrope rty);
        }

        Display(this);
        };

        3. Use "closures" (of which global level variables are an example in
        fact).

        Generally a variable defined in a parent function is available to a
        sub-function.

        Thus:-

        <script>
        function Parent()
        {
        var nParentVar=1;

        function Child()
        {
        alert(nParentVa r);
        }

        }

        Parent();

        </script>

        With this you can prime functions with objects (called "currying") .

        <html>
        <head>
        <script>

        function Initialise(eEle ment)
        {
        var oStyle=new Object;

        eElement.onclic k=function()
        {
        eElement.style. backgroundColor =oStyle.sColor;
        };

        return oStyle;
        };

        function Start()
        {
        var eDiv=document.g etElementById(" myDiv");
        var oStyle=Initiali se(eDiv);
        oStyle.sColor=" red";
        }

        </script>
        </head>
        </body>
        <p onclick="fStart (this)">Click to initialse DIV handler</p>

        <div id="myDiv" style="width:10 0; height:100; border-width:1;
        border-style:solid; border-color:black;"></div>

        </body>
        </html>



        [color=blue]
        > By the way, Yes, I know I can simply use <input type="text"
        > id="TextBox1" onclick="DoFunc tion();" />
        > But that's not what I want in this case.[/color]

        In fact, what is happening is that "DoFunction ()" is wrapped in an
        anonymous function.

        Equivalent to:

        TextBox1.onlcli ck=function()
        {
        DoFunction();
        };

        Comment

        Working...