addEventHandler / addEvent conflict

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

    #1

    addEventHandler / addEvent conflict


    All,

    I have a generic script to handle events. (NN/IE only)

    function addEvent(elemen tObject, eventName, functionObject)
    {
    if(document.add EventListener)
    elementObject.a ddEventListener (eventName, functionObject, false);
    else
    if(document.att achEvent)
    elementObject.a ttachEvent("on" + eventName, functionObject) ;
    }

    Now, what I wanted to do was pass a parameter to the functionObject, but
    this is apparently not possible.
    How can I determine what object caused the event in the functionObj?
    FWIU, the "this" object is fine in NN, but in IE, it refers to window.

    addEvent(docume nt.getElementBy Id("myE"),"clic k",myFunctio n);
    function myFunction()
    {
    // I need a reference to document.getEle mentById("myE") or whatever
    object triggered this function to be called
    }

    If someone knows a way to pass a parameter, that is preferable. :)

    TIA
    --
    --
    ~kaeli~
    Local Area Network in Australia:... the LAN down under.



  • Martin Honnen

    #2
    Re: addEventHandler / addEvent conflict



    kaeli wrote:
    [color=blue]
    > I have a generic script to handle events. (NN/IE only)
    >
    > function addEvent(elemen tObject, eventName, functionObject)
    > {
    > if(document.add EventListener)
    > elementObject.a ddEventListener (eventName, functionObject, false);
    > else
    > if(document.att achEvent)
    > elementObject.a ttachEvent("on" + eventName, functionObject) ;
    > }
    >
    > Now, what I wanted to do was pass a parameter to the functionObject, but
    > this is apparently not possible.
    > How can I determine what object caused the event in the functionObj?
    > FWIU, the "this" object is fine in NN, but in IE, it refers to window.[/color]

    With IE the element the event is fired on is
    window.event.sr cElement
    with a DOM compliant browser your functionObject should have a parameter say
    function functionObject (evt) {...}
    and then you can access
    evt.target
    as the node the event was fired on.
    As you are asking for the this object, that would be
    evt.currentTarg et

    For IE you will need a closure e.g.

    function addEvent(elemen tObject, eventName, functionObject)
    {
    if(document.add EventListener)
    elementObject.a ddEventListener (eventName,
    function (evt) {
    functionObject( elementObject, evt)
    }, false);
    else
    if(document.att achEvent)
    elementObject.a ttachEvent("on" + eventName,
    function () {
    functionObject( elementObject);
    }
    )
    }


    with
    function myFunction (element, evt) {
    // access element here if needed
    }
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • kaeli

      #3
      Re: addEventHandler / addEvent conflict

      In article <400574cc$1@ola f.komtel.net>, mahotrash@yahoo .de enlightened
      us with...[color=blue]
      >
      >
      > function addEvent(elemen tObject, eventName, functionObject)
      > {
      > if(document.add EventListener)
      > elementObject.a ddEventListener (eventName,
      > function (evt) {
      > functionObject( elementObject, evt)
      > }, false);
      > else
      > if(document.att achEvent)
      > elementObject.a ttachEvent("on" + eventName,
      > function () {
      > functionObject( elementObject);
      > }
      > )
      > }
      >
      >
      > with
      > function myFunction (element, evt) {
      > // access element here if needed
      > }
      >[/color]

      Worked great, thanks!

      For archival purposes, in the html, I named the elements with a number
      at the end, then in myFunction used
      element.name.su bstring(element .name.length-1)
      to retrieve the number.
      The purpose: correspondingly named textboxes and selects were being
      synchronized and I wanted the function to be able to be used for all of
      them.

      textbox1, select1
      textbox2, select2
      etc
      The function could then tell which text element to modify when a
      particular select element was changed. Previously a reference to both
      objects had been passed.

      --
      --
      ~kaeli~
      Dancing cheek-to-cheek is really a form of floor play.



      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: addEventHandler / addEvent conflict

        kaeli <tiny_one@NOSPA M.comcast.net> writes:
        [color=blue]
        > I have a generic script to handle events. (NN/IE only)[/color]

        NN? Not Netscape 4 for sure, but it works in all DOM 2 compliant browsers,
        which include Opera 7.
        [color=blue]
        > function addEvent(elemen tObject, eventName, functionObject)
        > {
        > if(document.add EventListener)
        > elementObject.a ddEventListener (eventName, functionObject, false);
        > else
        > if(document.att achEvent)
        > elementObject.a ttachEvent("on" + eventName, functionObject) ;
        > }[/color]

        Yes, this will work in most cases.
        [color=blue]
        > Now, what I wanted to do was pass a parameter to the functionObject, but
        > this is apparently not possible.[/color]

        It already gets one argument, the event. There is no direct way to store
        another value (or more) with it.
        [color=blue]
        > How can I determine what object caused the event in the functionObj?[/color]

        Normally (if assigned to the "onclick" attribute or with
        addEventListene r), "this" would refer to the element, because the
        function is treated as a method of the element object.
        [color=blue]
        > FWIU, the "this" object is fine in NN, but in IE, it refers to window.[/color]

        ..... but not with attachEvent.
        [color=blue]
        > addEvent(docume nt.getElementBy Id("myE"),"clic k",myFunctio n);
        > function myFunction()
        > {
        > // I need a reference to document.getEle mentById("myE") or whatever
        > object triggered this function to be called
        > }[/color]
        [color=blue]
        > If someone knows a way to pass a parameter, that is preferable. :)[/color]

        Try this general function instead:
        ---
        function addEvent(elemen tObject, eventName, functionObject /* , ... */)
        {
        var args = Array.prototype .slice.call(arg uments,3);
        function wrapper(event) {
        return functionObject. apply(elementOb ject,[event].concat(args));
        }
        if (document.addEv entListener) {
        elementObject.a ddEventListener (eventName, wrapper, false);
        } else if (document.attac hEvent) {
        elementObject.a ttachEvent("on" + eventName, wrapper);
        }
        }
        ---
        It has the disadvantage of not allowing detachEvent or removeEventList ener
        to remove the function. For that you need something more complicated, but
        doable.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • kaeli

          #5
          Re: addEventHandler / addEvent conflict

          In article <hdyyw90u.fsf@h otpop.com>, lrn@hotpop.com enlightened us
          with...[color=blue]
          > kaeli <tiny_one@NOSPA M.comcast.net> writes:
          >[color=green]
          > > I have a generic script to handle events. (NN/IE only)[/color]
          >
          > NN? Not Netscape 4 for sure, but it works in all DOM 2 compliant browsers,
          > which include Opera 7.
          >[/color]

          Sorry, clarification: NN6+/IE5+ only are the only ones tested and the
          only ones I care about. Just didn't want people telling me about all the
          browsers it didn't work in. Intranet. *g*
          [color=blue]
          > It has the disadvantage of not allowing detachEvent or removeEventList ener
          > to remove the function.[/color]

          I don't need to and Martin's function worked great. :)
          This was a workaround so I could have my taglibraries (JSP) generate my
          form elements (dynamically made as normal or readonly based on user's
          permissions). The problem was that I didn't want to attach javascript
          from the tag libraries, as the elements aren't always scripted the same
          way and I wanted the script to be modifiable and removable with no
          modification to the taglib. So I needed the elements and the script
          separate.

          Thanks!

          --
          --
          ~kaeli~
          If the funeral procession is at night, do folks drive with
          their lights off?



          Comment

          Working...