clicking on list elements - problem

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

    #31
    Re: clicking on list elements - problem

    > [3][color=blue]
    > ...OuterLayer onclick="update (event.target, this, newContent)"...[/color]

    Actually here is FF who doesn't act by ECMA, because it doesn't let you
    forward an argument from the outer function to the inner one.
    ....onclick="up date(event, this, newContent)... - this will fail
    ("event" is UOA - Undefined On Arrival :)
    You have to pull out the needed properties right on the spot, or you
    are out of luck. The fact that obj.onclick.arg uments[0] works like a
    charm - it shows that this might be not an error, but an intentional
    block. But why?

    Comment

    • Michael Winter

      #32
      Re: clicking on list elements - problem

      On 25/05/2005 13:51, VK wrote:
      [color=blue]
      > 1. About the nature of the event object in IE:[/color]

      I'm not going to comment on this. As I said previously, it's entirely
      irrelevant.

      [snip]
      [color=blue]
      > Dear Michael, I know very well how to get event properties in a FF/IE
      > compatible way. [...][/color]

      But there seems to be some doubt about that, otherwise you wouldn't be
      posting a question that I already answered.

      [snip]
      [color=blue]
      > ...OuterLayer onclick="update (event.target, this, newContent)"...
      > ...
      > function update(tar, obj, content) {
      > /* It works, it's ECMA-obeyant, but the first argument is reserved for
      > POSSIBLE argument */[/color]

      Only because *you've* chosen to reserve the first argument. Either you
      didn't read my previous post, or you didn't understand it. I will try
      one last time. If you don't get it this time, I give up.

      Say that we're going to add this onclick event via addEventListene r (I'm
      skipping feature detection and whatnot for simplicity):

      var outerLayer = document.getEle mentById('Outer Layer');

      outerLayer.addE ventListener('c lick', listener, false);

      When listener is called, it will be passed the event object as it's
      first argument, so we need a function along the lines of:

      function listener(e) {
      /* ... */
      }

      Now we call your update function with the following signature:

      function update(object, content, event) {}

      with

      function listener(e) {
      update(this, '...', e);
      }

      OK? That's simple enough.


      Now instead, we'll use the onclick attribute. The process is exactly the
      same (!) with only one difference: the user agent will create 'listener'
      for us internally, with 'event' instead of 'e':

      onclick="update (this, '...', event);"

      That's it. Do you see? The onclick attribute just specifies code for the
      body of a generated function. What you put in that function body is up
      to you - you can pass the event object in any way you choose. It's your
      decision.

      I already said this in my previous post, and I think I've made it as
      clear as I possibly can. If you don't understand, well, I'm really not
      sure what else I can do, especially if you just keep repeating yourself
      or going off on tangents, rather than helping me to help you.


      Using what I wrote above, your update function would look something like:

      /* The event argument is optional. If missing,
      * the content will be prepended to the object
      * unconditionally .
      */
      function update(object, content, event) {
      var target;

      /* First, check if an event object was passed to the
      * function. If so, attempt to obtain the target
      * element of that event.
      */
      if(event) {target = event.target || event.srcElemen t;}

      /* If no target was found (because no event object
      * was passed), OR the target was the object argument,
      * prepend content to object.
      */
      if(!target || (target == object)) {
      object.innerHTM L = content + object.innerHTM L;
      }
      }

      Even if you still haven't understood (and I hope to &deity; that you
      have), just try what I've suggested. You'll see that it works.

      Mike

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

      Comment

      • Thomas 'PointedEars' Lahn

        #33
        Re: clicking on list elements - problem

        VK wrote:
        [color=blue]
        > The best trick for all this event mess so far I found is from Lasse
        > Reichstein Nielsen:
        > ...
        > <li id="i" onclick="show(t his, (event.target|| event.srcElemen t))">Some
        > text­</li>
        > ...[/color]

        That is not the best way. Consider that, however unlikely, the `event'
        reference is `undefined' or `null', this would result in a script error;
        what you are relying on in the above is still non-standard host-specific
        behavior.

        The below `show' method, on the other hand, can check whether the passed
        argument is `undefined' or `null' before accessing its properties. That
        would result in:
        [color=blue]
        > function show(src,trg) {
        > if (src == trg) {
        > alert(src.id);
        > }
        > }[/color]

        <meta http-equiv="Content-Script-Type" content="text/javascript">
        <script type="text/javascript">
        function show(src, e)
        {
        var trg = e && (e.target || e.srcElement);
        if (trg && (src == trg))
        {
        alert(src.id);
        }
        }
        </script>
        ...
        <li id="i" onclick="show(t his, event)">Some text­</li>
        [color=blue]
        > Works for IE 6.x and FF 1.0.3/4[/color]

        And no more is for sure without change.


        PointedEars

        Comment

        • Thomas 'PointedEars' Lahn

          #34
          Re: clicking on list elements - problem

          VK wrote:
          [color=blue][color=green]
          >> [3]
          >> ...OuterLayer onclick="update (event.target, this, newContent)"...[/color]
          >
          > Actually here is FF who doesn't act by ECMA, because it doesn't let you
          > forward an argument from the outer function to the inner one.[/color]

          But it does.
          [color=blue]
          > ...onclick="upd ate(event, this, newContent)... - this will fail
          > ("event" is UOA - Undefined On Arrival :)[/color]

          No, it is not. Either your test case is flawed or your Firefox nightly
          is buggy.
          [color=blue]
          > You have to pull out the needed properties right on the spot, or you
          > are out of luck.[/color]

          No, it works long since without referring to properties of `event'
          within the event handler attribute value and that did not change
          to date.


          PointedEars
          --
          When the power of love overcomes the love
          of power, the world will know peace.
          -- Jimi Hendrix

          Comment

          Working...