clicking on list elements - problem

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

    #16
    Re: clicking on list elements - problem

    Andy LW wrote:

    [snip]
    [color=blue]
    > could you please explain this line (or point to some online source that
    > could help me understand [an if statement][/color]

    Essentially, the expression gets a reference to the event target, and checks
    that this target is a list item (LI) element. I'll walk through it in
    detail.

    document.onclic k = function(e) {
    var tgt;
    [color=blue]
    > if ((e = e || window.event)[/color]

    If you've managed to wade through the silliness, you will hopefully have
    grasped that there are basically two event models: Netscape and IE.

    The Netscape model passes the event object to a listener as an argument,
    whilst IE provides a global object. So, the first thing we need to do is
    get this object in one place. The expression

    (e = e || window.event)

    is fairly simple. The logical OR operator evaluates its operands as
    booleans. If the first operand evaluates to true, the /value/ of that
    operand will be the result. If the first operand evaluates to false, the
    value of the second operand will be the result. In other words, if e is not
    an object reference, replace its value with that of window.event.
    [color=blue]
    > &&[/color]

    Before moving on to the next expression, we need to make sure that e
    actually contains an event object. To do this, we use the logical AND
    operator to ensure that all of the expressions in the if statement evaluate
    to true.
    [color=blue]
    > (tgt = e.srcElement || e.target)[/color]

    As part of the difference in event models, both use a different property to
    refer to the target of an event. The Netscape/W3C model uses target, whilst
    IE uses srcElement. The tgt variable will be assigned one of these
    values...
    [color=blue]
    > && /LI/i.test(tgt.node Name))[/color]

    Finally, this regular expression checks the nodeName property of the event
    target. The nodeName property contains the tag name for elements (and other
    values for other node types). We want to ensure that the event was
    targeting a list item (LI), so the expression makes sure that the node name
    contains this value.

    The regular expression contains the i (case-insensitive) flag. The reason
    for this is that the nodeName value can vary depending on whether a
    document is HTML or XHTML. With HTML (and XHTML served as HTML), a tag name
    (like LI) is always uppercase, and values for other node types (like #text)
    are lowercase. With XML-based documents (namely XHTML), the nodeName value
    uses the case of the element as XML, unlike SGML, is case-sensitive. This
    means that elements borrowed from HTML (like P, TABLE, etc.) are always
    lowercase, elements defined in other namespaces may vary. Using a
    case-insensitive comparison sidesteps all of this.

    I hope that made sense.

    Mike

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

    Comment

    • RobG

      #17
      Re: clicking on list elements - problem

      VK wrote:[color=blue][color=green]
      >>Does adding an event handler dynamically
      >>avoid intrinsic events, or is it just a different way of adding them?[/color]
      >
      >
      > Using intrinsic events forces you to use the non-documented EVENT
      > object in FF (Some people tried to say here that this is some special
      > EVENT and it has nothing to do by its nature with window.event in IE).[/color]

      Not documented? A quick search of the Mozilla site yielded:

      <URL:http://devedge-temp.mozilla.or g/library/manuals/2000/javascript/1.3/guide/evnt.html>

      <URL:http://www.mozilla.org/docs/dom/domref/examples.html#9 99002>

      <URL:http://www.mozilla.org/docs/dom/domref/examples8.html>

      <URL:http://www.w3.org/TR/DOM-Level-2-Events/events.html#Eve nts-Event>

      You have yet to show any evidence that coding events inline is more
      'unstable' than adding them dynamically. Adding events dynamically is
      more aligned with 'best practice', but that is not the point. Adding
      events inline does not 'force' you to use the event object, nor is it
      'unstable'.

      The difference is that with IE the event object is global, with Mozilla
      it is local and therefore you have to access it differently - for the
      vast majority of scripts, you don't need to bother with it at all.
      [color=blue]
      >
      > <html>
      > <head>
      > <title>Event</title>
      > <meta http-equiv="Content-Type" content="text/html;
      > charset=iso-8859-1">
      > </head>
      > <body>
      > <p onclick="alert( event.target)"> Test</p>
      > </body>
      > </html>
      >
      > Click and get it. I did not create this EVENT object, nevertheless it's[/color]

      Have a look at the properties of an HTML element's style object. You
      didn't 'create' them, yet they are there. They are accessible on every
      element, but they aren't global.

      <p id="x">This is a paragraph</p>
      <p id="y"></p>
      <script type="text/javascript">
      var x = document.getEle mentById('x');
      var z = [];
      for (prop in x.style){
      z.push( prop + ': ' + ( x.style[prop] || '&nbsp;<i>not set</i>' ) );
      }
      document.getEle mentById('y').i nnerHTML = z.join('<br>');
      </script>

      [color=blue]
      > available and self-updating. Thus it's a global public self-updating
      > event object.[/color]

      No, in the Mozilla model it's a local object belonging to each event.
      [color=blue]
      > Thus it's equal to windows.event in IE.[/color]

      In my mind it's equivalent to, not equal to.

      [...]

      --
      RobG

      Comment

      • VK

        #18
        Re: clicking on list elements - problem

        > Not documented? A quick search of the Mozilla site yielded:[color=blue]
        >[/color]
        <URL:http://devedge-temp.mozilla.or ­g/library/manuals/2000/javascr­ipt/1.3/g...>
        [color=blue]
        > <URL:http://www.mozilla.org/docs/do­m/domref/examples.html#9 99002>
        > <URL:http://www.mozilla.org/docs/do­m/domref/examples8.html>
        >[/color]
        <URL:http://www.w3.org/TR/DOM-Level­-2-Events/events.html#Eve nts-E­vent>

        Did you actually read these docs or just checked for presence of the
        word "Event"? We are not talking about "Event" as a conceptual
        definition, we are talking about THAT event, which is in <...
        onclick="alert( event.target)">
        Taking this into account, read these docs once again. And don't forget
        the main one:
        <http://www.mozilla.org/docs/dom/domref/dom_event_ref.h tml#998197>

        If you find anything about THAT event, send me your PanPal acc#, I owe
        you 10 bucks.

        Comment

        • VK

          #19
          Re: clicking on list elements - problem

          >> Thus it's a global public self-updating event object.[color=blue]
          > That's a leap which you cannot support.[/color]

          Actually I can ;-) This object is just a little bit under-done, and for
          more confusion is hidden under a bunch of university terms.
          Try this in FF:
          ....
          function test() {
          var obj = document.getEle mentById('p1');
          alert(obj.oncli ck.arguments[0].currentTarget. innerHTML);
          }
          ....
          <p id="p1" onclick="test2( )">Click me</p>
          ....

          All they had to do: make an anonymous forwarding of event reference (as
          arguments[0]) to the event handler. They already did it for the event
          capturer, so having said "A" say "B" also. So we would not need to use
          this sidewalk, and we would have a full equivalent of IE scheme. OK,
          methods are called differently, but we are living with it since the
          beginning of times.
          [color=blue]
          > Why is this so difficult to comprehend?[/color]
          It IS difficult to comprehend: the 'event' you can learn only from the
          street, the "Russian hills rider" from the official docs, and a bunch
          of details which are not covered or covered wrongfully. I'm maybe not
          the smartest one here, but still BS in linguistics... It took nearly 5
          days of intensive reading and posting to get the picture clear.

          Comment

          • Michael Winter

            #20
            Re: clicking on list elements - problem

            On 20/05/2005 18:11, VK wrote:
            [color=blue][color=green][color=darkred]
            >>> Thus it's a global public self-updating event object.[/color]
            >>
            >> That's a leap which you cannot support.[/color]
            >
            > Actually I can ;-)[/color]

            No, you can't (and haven't). In the Netscape/W3C model, the event object
            is local, not local. It could be a single object that is altered when
            passed between elements, but it doesn't have to be, so 'self-updating'
            doesn't necessarily apply, either. As for 'public', I'm not quite sure
            what you're trying to imply. It's as public as any local variable can be.

            [snip]
            [color=blue]
            > function test() {
            > var obj = document.getEle mentById('p1');
            > alert(obj.oncli ck.arguments[0].currentTarget. innerHTML);
            > }
            > ...
            > <p id="p1" onclick="test2( )">Click me</p>[/color]

            What do you think that proves, aside from the fact that deprecated usage
            of the arguments object is still supported? The behaviour has nothing to
            do with the involvement of the event object:

            function a() {
            b();
            }
            function b() {
            alert(a.argumen ts[0]);
            }

            a('some value');

            You're simply accessing the arguments object as a property of some
            function within the execution context stack - a feature which is not
            specified in ECMA-262 3rd Ed.

            [snip]
            [color=blue][color=green]
            >> Why is this so difficult to comprehend?[/color]
            >
            > It IS difficult to comprehend: the 'event' you can learn only from the
            > street [...][/color]

            There will have been examples and articles produced for the past decade,
            or so, which demonstrate the usage that's been described. It's been
            around from at least NN4; possibly earlier. I agree that it should have
            been included in some 'official' documentation, but it hasn't. If you're
            that concerned, talk to the Mozilla documentation team and get them to
            describe it in their Web Developer section.

            [snip]

            Mike

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

            Comment

            • Andy LW

              #21
              Re: clicking on list elements - problem

              "Michael Winter" <m.winter@bluey onder.co.invali d> wrote in message
              news:52kje.3585 1$G8.9692@text. news.blueyonder .co.uk...[color=blue]
              > Andy LW wrote:
              >
              > [snip]
              >[color=green]
              >> could you please explain this line (or point to some online source that
              >> could help me understand [an if statement][/color]
              >
              > Essentially, the expression gets a reference to the event target, and
              > checks
              > that this target is a list item (LI) element. I'll walk through it in
              > detail.
              >
              > document.onclic k = function(e) {
              > var tgt;
              >[color=green]
              >> if ((e = e || window.event)[/color]
              >
              > If you've managed to wade through the silliness, you will hopefully have
              > grasped that there are basically two event models: Netscape and IE.
              >
              > The Netscape model passes the event object to a listener as an argument,
              > whilst IE provides a global object. So, the first thing we need to do is
              > get this object in one place. The expression
              >
              > (e = e || window.event)
              >
              > is fairly simple. The logical OR operator evaluates its operands as
              > booleans. If the first operand evaluates to true, the /value/ of that
              > operand will be the result. If the first operand evaluates to false, the
              > value of the second operand will be the result. In other words, if e is
              > not
              > an object reference, replace its value with that of window.event.
              >[color=green]
              >> &&[/color]
              >
              > Before moving on to the next expression, we need to make sure that e
              > actually contains an event object. To do this, we use the logical AND
              > operator to ensure that all of the expressions in the if statement
              > evaluate
              > to true.
              >[color=green]
              >> (tgt = e.srcElement || e.target)[/color]
              >
              > As part of the difference in event models, both use a different property
              > to
              > refer to the target of an event. The Netscape/W3C model uses target,
              > whilst
              > IE uses srcElement. The tgt variable will be assigned one of these
              > values...
              >[color=green]
              >> && /LI/i.test(tgt.node Name))[/color]
              >
              > Finally, this regular expression checks the nodeName property of the event
              > target. The nodeName property contains the tag name for elements (and
              > other
              > values for other node types). We want to ensure that the event was
              > targeting a list item (LI), so the expression makes sure that the node
              > name
              > contains this value.
              >
              > The regular expression contains the i (case-insensitive) flag. The reason
              > for this is that the nodeName value can vary depending on whether a
              > document is HTML or XHTML. With HTML (and XHTML served as HTML), a tag
              > name
              > (like LI) is always uppercase, and values for other node types (like
              > #text)
              > are lowercase. With XML-based documents (namely XHTML), the nodeName value
              > uses the case of the element as XML, unlike SGML, is case-sensitive. This
              > means that elements borrowed from HTML (like P, TABLE, etc.) are always
              > lowercase, elements defined in other namespaces may vary. Using a
              > case-insensitive comparison sidesteps all of this.
              >
              > I hope that made sense.
              >
              > Mike
              >
              > --
              > Michael Winter
              > Replace ".invalid" with ".uk" to reply by e-mail.[/color]


              thank you very, very much for your patience and detailed explanations. that
              was much more than i expected to get. you left nothing unexplained and
              unclear. thank you again.


              Comment

              • VK

                #22
                Re: clicking on list elements - problem

                If anyone is still watching this thread:
                A week ago I took a small tech-writing gig for a DLL library. That put
                me "face down" to the background life of all this stuff.

                I have to admit that my "public self-updating event object" is
                total bs (and unlike the science degree, this "bs" is all in small
                letters and staying from other famous words :-(

                Michael Winter tried to explain this to me, but I did not get it.

                There is no "public self-updating event object" neither in IE, nor
                in FF, nor in any other browser.
                There is unique Event object, that acts almost exactly like the Date
                object.
                The Date object is always "ticking" counting milliseconds. If we do
                new Date(), the Date object creates a copy of itself with the
                "timestamp" of the moment of the creation. We receive a reference
                on this copy for further work. Evidently the original and the copy are
                equal to each other, but just one millisecond later they are not (the
                Date keeps ticking, the timestamp is frozen).

                The Event object is continuously changing, passing through itself all
                incoming events. At the moment of any event it checks for registered
                event listeners of this particular event type. If there is at least
                one, then the Event creates a momentary copy of itself, we could call
                it "eventstamp ". This "eventstamp " is being delivered to each
                listener in some predefined order. There only 3 differences between IE
                and FF:

                1. Event delivery goes down->up in IE and up->down->up in FF (double
                pass).

                2. In IE eventstamp appears as automatically created variable
                "event" in the body of the function.
                In FF eventstamp appears as the first argument of the function, also in
                intrinsic functions this argument as always called "event". In
                regular functions you can call it however you want.

                3. In IE the "event" variable is created automatically for the
                outer function as well as for all inner functions. In FF you have to
                save needed event properties right away, or access them in indirect way
                like outerFunction.a rguments[0].someEventPrope rty.

                How does it really work - just in case if you are still curious (I
                was). On the source code level there is a Message Dispatcher. It
                receives messages (events) from the OS and forms the messages queue.[color=blue]
                >From that queue it takes the oldest one and goes to the special table,[/color]
                where the program keeps all registered message receivers. Program sends
                messages to these receivers one-by-one in the predefined order, based
                on some receivers properties.

                Comment

                • Lasse Reichstein Nielsen

                  #23
                  Re: clicking on list elements - problem

                  "VK" <schools_ring@y ahoo.com> writes:
                  [color=blue]
                  > There is no "public self-updating event object" neither in IE, nor
                  > in FF, nor in any other browser.[/color]

                  Indeed. If you check what happens in IE, there is a global "event"
                  variable. Its content changes when new events are triggered, meaning
                  it is a new event object each time.
                  [color=blue]
                  > There is unique Event object, that acts almost exactly like the Date
                  > object.[/color]

                  There is no global variable called "Event" in IE, although I believe
                  there is in Gecko-based browsers. There it can be used as a constructor
                  function for event objects ... something like Date for date objects, yes.
                  [color=blue]
                  > The Date object is always "ticking" counting milliseconds. If we do
                  > new Date(), the Date object creates a copy of itself with the
                  > "timestamp" of the moment of the creation.[/color]

                  Uhm, no.
                  The Date object is a single function object. It does nothing by itself.

                  When you create a new object based on the Date function, the new
                  object inherits methods from Date.prototype. It is also assigned a
                  value that is the current time, as reported by the operating system
                  (the Date function is a host object, so it can access non-Javascript
                  resources).
                  [color=blue]
                  > We receive a reference on this copy for further work. Evidently the
                  > original and the copy are equal to each other, but just one
                  > millisecond later they are not (the Date keeps ticking, the
                  > timestamp is frozen).[/color]

                  No, they are even different types.

                  The date object created is an object. The Date function is (duh :) a
                  function. Test it:
                  alert([typeof Date, typeof new Date()]);

                  The Date function os more like a template, while the date object is a
                  real object created from the mold of the template. They are no more
                  equal than a cooike and a cookie cutter.
                  [color=blue]
                  > The Event object is continuously changing, passing through itself all
                  > incoming events. At the moment of any event it checks for registered
                  > event listeners of this particular event type. If there is at least
                  > one, then the Event creates a momentary copy of itself, we could call
                  > it "eventstamp ". This "eventstamp " is being delivered to each
                  > listener in some predefined order.[/color]

                  More likely: the browser is internally listening for input events from
                  the operating system. When applicable, it converts those events to DOM
                  events (an OS mouse click to a DOM "click" event). The browser creates
                  new DOM event objects as needed.
                  [color=blue]
                  > There only 3 differences between IE
                  > and FF:
                  >
                  > 1. Event delivery goes down->up in IE and up->down->up in FF (double
                  > pass).[/color]

                  Sounds correct. The Gecko engine uses the W3C DOM 2 Events model. IE
                  uses their own "bubbling" model (which predates DOM 2, so they were
                  probably an inspiration to DOM 2 Events).
                  [color=blue]
                  > 2. In IE eventstamp appears as automatically created variable
                  > "event" in the body of the function.[/color]

                  No. The current event is assigned to a global variable called "event"
                  before bubbling starts. The variable is not local to the body of the
                  function.
                  [color=blue]
                  > In FF eventstamp appears as the first argument of the function, also in
                  > intrinsic functions this argument as always called "event". In
                  > regular functions you can call it however you want.[/color]

                  Correct (where "intrinsic functions" are the JS functions created by
                  the browser from the HTML intrinsic event handler attributes).
                  [color=blue]
                  > 3. In IE the "event" variable is created automatically for the
                  > outer function as well as for all inner functions.[/color]

                  It's a global variable, so it's visible everywhere, except when
                  shadowed by a local variable with the same name.
                  [color=blue]
                  > In FF you have to
                  > save needed event properties right away, or access them in indirect way
                  > like outerFunction.a rguments[0].someEventPrope rty.[/color]

                  Not a recommende way of accessing parameters of a function. It doesn't
                  work well with recursive functions, and it's not part of the ECMAScript
                  standard.
                  [color=blue]
                  > How does it really work - just in case if you are still curious (I
                  > was). On the source code level there is a Message Dispatcher. It
                  > receives messages (events) from the OS and forms the messages queue.[/color]

                  Agree.
                  [color=blue]
                  > From that queue it takes the oldest one and goes to the special table,
                  > where the program keeps all registered message receivers. Program sends
                  > messages to these receivers one-by-one in the predefined order, based
                  > on some receivers properties.[/color]

                  Vague, so probably correct :)

                  /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

                  • VK

                    #24
                    Re: clicking on list elements - problem

                    > There is no global variable called "Event" in IE, although I believe
                    there is in Gecko-based browsers.

                    Should we say "There is an event dispatcher in all browsers that..."
                    This way we are escaping a confusion between the Event as a programming
                    term definition (like Function, Subroutine, Loop etc) and some real
                    entity.
                    [color=blue]
                    > The current event is assigned to a global variable called "event"[/color]
                    before bubbling starts.

                    Don't agree. It is rather simple to recreate a situation when we are
                    handling several events at once (say drag and mousemove). It would mean
                    that we have 2 global vars with the same name and different values
                    (nonsense). Actually Microsoft says the same on their usual baby-talk:
                    "The event object is available only during an event-that is, you can
                    use it in event handlers but not in other code".
                    <http://msdn.microsoft. com/library/default.asp?url =/workshop/author/dhtml/reference/objects/obj_event.asp>
                    So the eventstamp "event" acts as private variable: generic to the
                    event handler and its inner functions.

                    [color=blue][color=green]
                    >> outerFunction.a rguments[0].som­eEventProp erty.[/color]
                    > Not a recommende way[/color]

                    Just thinking off a way to work with intrinsic event handler having
                    some arguments to pass to other function:

                    <div onclick="f1(arg 0,arg1,arg3)"> - no problem with IE, but in FF arg0
                    must be sacrificed to event/event property. It is not very convenient
                    to reserve first argument in each function for e, just in case if it
                    will be called from an intrinsic event handler.
                    ?

                    Comment

                    • Random

                      #25
                      Re: clicking on list elements - problem

                      window.event always exists in IE, though outside of an event handler
                      (or recursively-called functions) its value is reported as null. It
                      doesn't seem possible to retrieve a reference to the window.event
                      object -- it always gives back a copy of itself (see code segment
                      below).

                      Based on this and several other quick-and-dirty experiments, it seems
                      most likely that IE has an internal class it uses to represent an
                      event, which it instantiates and exposes as window.event. When the
                      event's registered handler finishes, the value of window.event is
                      reverted to null (rather than a pointer or reference to an internal IE
                      object). The actual object is destroyed per normal garbage collection:
                      when its internal reference counter reaches 0.

                      It is also possible that it's not exposing an internal object but
                      instantiating a JavaScript object, but I doubt it-- trying to set
                      ..srcElement to a string yields 'type mismatch', and error we're not
                      very used to seeing in JS.

                      Because of the garbage collection, it is possible to have two 'Event
                      objects' instantiated (by keeping at least one reference to an event
                      that was already processed), but I'm not certain that there is a way to
                      make two events _fire_ simultaneously. I've never tried on a
                      dual-processor machine. I did attempt in a very cursory fashion to make
                      IE process two events simultaneously, but it wouldn't do it. It wanted
                      to handle each sequentially.
                      [color=blue]
                      > It would mean that we have 2 global vars with the same name and
                      > different values (nonsense).[/color]

                      Not necessarily.

                      An onClick for a submit button is processed in its entirety before IE
                      'bubbles' upward, instantiating a new Event object and referencing it
                      with window.event then calling the form's onClick. When the form's
                      onClick return()s, IE 'bubbles' again, instantiating a new Event
                      object, to the body. When bubbling has finished, it instantiates an
                      Event and calls the form's registered onSubmit handler.

                      In fact, I couldn't get IE to run any other code while a handler was
                      running, so I couldn't get a better look at some of the finer details
                      in question here.

                      Point being, only one value for window.event is ever required at any
                      given moment.


                      Code segment referenced above:
                      <html>
                      <script>

                      var x = window.event;
                      var y;

                      function onload() {
                      y = window.event;
                      alert( x );
                      setTimeout( 'alert(y)', 1000 );
                      setTimeout( 'alert(x)', 2000 );
                      setTimeout( 'alert(window.e vent == y)', 3000 );
                      }

                      </script>
                      <body onload=onload() />
                      </html>


                      VK wrote:[color=blue][color=green]
                      > > There is no global variable called "Event" in IE, although I[/color][/color]
                      believe[color=blue]
                      > there is in Gecko-based browsers.
                      >
                      > Should we say "There is an event dispatcher in all browsers that..."
                      > This way we are escaping a confusion between the Event as a[/color]
                      programming[color=blue]
                      > term definition (like Function, Subroutine, Loop etc) and some real
                      > entity.
                      >[color=green]
                      > > The current event is assigned to a global variable called "event"[/color]
                      > before bubbling starts.
                      >
                      > Don't agree. It is rather simple to recreate a situation when we are
                      > handling several events at once (say drag and mousemove). It would[/color]
                      mean[color=blue]
                      > that we have 2 global vars with the same name and different values
                      > (nonsense). Actually Microsoft says the same on their usual[/color]
                      baby-talk:[color=blue]
                      > "The event object is available only during an event-that is, you can
                      > use it in event handlers but not in other code".
                      >[/color]
                      <http://msdn.microsoft. com/library/default.asp?url =/workshop/author/dhtml/reference/objects/obj_event.asp>[color=blue]
                      > So the eventstamp "event" acts as private variable: generic to the
                      > event handler and its inner functions.
                      >
                      >[color=green][color=darkred]
                      > >> outerFunction.a rguments[0].som­eEventProp erty.[/color]
                      > > Not a recommende way[/color]
                      >
                      > Just thinking off a way to work with intrinsic event handler having
                      > some arguments to pass to other function:
                      >
                      > <div onclick="f1(arg 0,arg1,arg3)"> - no problem with IE, but in FF[/color]
                      arg0[color=blue]
                      > must be sacrificed to event/event property. It is not very convenient
                      > to reserve first argument in each function for e, just in case if it
                      > will be called from an intrinsic event handler.
                      > ?[/color]

                      Comment

                      • Michael Winter

                        #26
                        Re: clicking on list elements - problem

                        On 24/05/2005 08:57, VK wrote:

                        [snip]
                        [color=blue][color=green]
                        >> The current event is assigned to a global variable called "event"
                        >> before bubbling starts.[/color]
                        >
                        > Don't agree. It is rather simple to recreate a situation when we are
                        > handling several events at once (say drag and mousemove).[/color]

                        Though reentrancy may occur, events are handled synchonously. As far as
                        a script is concerned, there is only one event to deal with at a time.
                        What occurs within the application is of no consequence to us.

                        [snip]
                        [color=blue]
                        > "The event object is available only during an event-that is, you can
                        > use it in event handlers but not in other code". [...][/color]

                        Microsoft mean that an event object doesn't persist once it has finished
                        bubbling through the document.
                        [color=blue]
                        > So the eventstamp "event" acts as private variable: generic to the
                        > event handler and its inner functions.[/color]

                        It has nothing to do with inner functions. In IE, the event object is
                        global. It's as simple as that. However, the object is transient, not
                        persistent.

                        [snip]
                        [color=blue]
                        > <div onclick="f1(arg 0,arg1,arg3)"> - no problem with IE, but in FF arg0
                        > must be sacrificed to event/event property.[/color]

                        Utter nonsense. Read how an intrinsic event listener is defined
                        internally, again. I've already demonstrated it twice in this thread.
                        I'm not doing it a third time.

                        [snip]

                        Mike

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

                        Comment

                        • Dr John Stockton

                          #27
                          Re: clicking on list elements - problem

                          JRS: In article <7jhqlzd2.fsf@h otpop.com>, dated Mon, 23 May 2005
                          17:46:01, seen in news:comp.lang. javascript, Lasse Reichstein Nielsen
                          <lrn@hotpop.com > posted :
                          [color=blue]
                          >When you create a new object based on the Date function, the new
                          >object inherits methods from Date.prototype. It is also assigned a
                          >value that is the current time, as reported by the operating system
                          >(the Date function is a host object, so it can access non-Javascript
                          >resources).[/color]


                          The latter only, however, if it is given no parameter.

                          If it is given a numeric parameter, its initialisation should be, and
                          IIRC on my system is, much faster, since it does not have to ask for the
                          time.

                          If it is given a string parameter, it has to work out what the parameter
                          means - and of course may get an answer other than the intended one (for
                          example, if the intended year was low-numbered).

                          --
                          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                          Comment

                          • Lasse Reichstein Nielsen

                            #28
                            Re: clicking on list elements - problem

                            "VK" <schools_ring@y ahoo.com> writes:
                            [color=blue]
                            > Should we say "There is an event dispatcher in all browsers that..."[/color]

                            It's true, so let's :)
                            [color=blue][color=green]
                            >> The current event is assigned to a global variable called "event"
                            >> before bubbling starts.[/color]
                            >
                            > Don't agree. It is rather simple to recreate a situation when we are
                            > handling several events at once (say drag and mousemove).[/color]

                            Nope. JScript is not multithreaded, and each event his handled
                            completely before the next is fired. You might trigger two events at
                            one time, but the JScript system fires the one at a time.
                            [color=blue]
                            > It would mean that we have 2 global vars with the same name and
                            > different values (nonsense).[/color]

                            Same variable, different time.
                            [color=blue]
                            > Actually Microsoft says the same on
                            > their usual baby-talk: "The event object is available only during an
                            > event-that is, you can use it in event handlers but not in other
                            > code".[/color]

                            Which is correct, since even if you can store a reference to the event
                            inside the event handler code, then using it later gives the values of the
                            new event, not the old one.
                            [color=blue]
                            > So the eventstamp "event" acts as private variable: generic to the
                            > event handler and its inner functions.[/color]

                            The name "private variable" has a quite different meaning. The "event"
                            variable is global. Its value is constrained to making sense only
                            during event handling.
                            [color=blue]
                            > <div onclick="f1(arg 0,arg1,arg3)"> - no problem with IE, but in FF arg0
                            > must be sacrificed to event/event property. It is not very convenient
                            > to reserve first argument in each function for e, just in case if it
                            > will be called from an intrinsic event handler.[/color]

                            If the function can be called both during event handling and not, then
                            it doesn't use the event, and there is no need to pass it as an
                            parameter.

                            If the function uses the event, then it should be called during event
                            handling, and I see no problem with passing it as a parameter. In fact,
                            I prefer the event as a paramter to using a global variable.

                            So, I can't see a problem here.

                            /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

                            • Michael Winter

                              #29
                              Re: clicking on list elements - problem

                              On 24/05/2005 08:57, VK wrote:
                              [color=blue]
                              > <div onclick="f1(arg 0,arg1,arg3)"> - no problem with IE, but in FF arg0
                              > must be sacrificed to event/event property.[/color]

                              As I said very gruffly previously (I apologise), this isn't true. For
                              simplicity, I'll refer to the IE and Netscape event models. This isn't
                              accurate as other user agents also implement these models; Opera, for
                              instance, implements both.

                              When a listener is attached directly to an element, its first argument,
                              under the Netscape model, will receive the event object. That is, if a
                              listener is added using the addEventListene r method, or if a reference
                              to a function object is assigned to an on<type> event property, then
                              that listener will only be called with one argument: the event object.

                              Code that is added via HTML attributes is different. It becomes the
                              function body of a listener created by the user agent itself. As such,
                              there are no constraints; you can do whatever you want with it. Do you
                              see the difference?

                              <div onclick="/* some code */">

                              and

                              <div id="myDiv">

                              document.getEle mentById('myDiv ').onclick = function(event) {
                              /* some code */
                              };

                              are identical from a practical point of view. If you display the value
                              of the onclick property in both cases in a user agent that follows the
                              Netscape model, you will see the same string value.

                              If you wanted to call a function from within these listeners to do the
                              real event handling, you don't have to pass the event object first. You
                              are free to define the code within these listeners as you please.

                              <div onclick="myFunc tion(event, this);">

                              and

                              <div onclick="myFunc tion(this, event);">

                              are both fine. The event identifier provided by a user agent won't
                              suddenly stop working if you use the latter.

                              The difference that does exist between the listener created by the user
                              agent when it parsed intrinsic event attribute, and those listeners that
                              you add directly yourself, is that the user agent always provides the
                              event object through the identifier, 'event'.

                              <div onclick="/* You must use 'event' here to access the object. */">

                              This is true under either event model.

                              When you add the listener yourself, you can use any identifier you like
                              under the Netscape model.

                              <div id="myDiv">

                              document.getEle mentById('myDiv ').onclick = function( whatever ) {
                              /* You now use 'whatever', here. */
                              };

                              Under the IE model, you must still use 'event':

                              document.getEle mentById('myDiv ').onclick = function() {
                              /* No argument is passed to this listener.
                              * You must use the global, 'event', instead.
                              */
                              };

                              So, combining them, you get:

                              document.getEle mentById('myDiv ').onclick = function( whatever ) {
                              whatever = whatever || event;
                              };


                              Unless you state what you don't understand about this, I doubt anyone
                              will be able to help you. I think you might be trying to over analyse it
                              all. Your theories as to what occurs within the application, for
                              example, are irrelevant. It has no effect on how you need to write event
                              handling code.

                              Mike

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

                              Comment

                              • VK

                                #30
                                Re: clicking on list elements - problem

                                1. About the nature of the event object in IE:
                                IE creates new public event object for each event. This object is
                                called "event" and it is available until anyone is using it. So it can
                                be any amount of event objects (up to the capacity of your program
                                stack). All these objects are equally named "event". Only one event
                                object is visible to the program context at the given time. Which one -
                                it depends on who is the original caller (aka original event owner).
                                Overall it's a re-incarnation of the application message queue.

                                Testcase (IE only of course):

                                <html>
                                <head>
                                <title>Event test 009</title>
                                <meta http-equiv="Content-Type" content="text/html;
                                charset=iso-8859-1">
                                <script>

                                function test1() {
                                document.getEle mentById('p2'). fireEvent('onmo useup');
                                alert(test2());
                                }

                                function test2() {
                                return event.srcElemen t.innerText;
                                }

                                function test3() {
                                alert(event.typ e);
                                }
                                </script>
                                </head>

                                <body bgcolor="#FFFFF F">
                                <p id="p1" onclick="test1( )">Line One</p>
                                <p id="p2" onmouseup="test 3()">Line Two</p>
                                </body>
                                </html>

                                Click on Line One, get the event type for Line Two, get the original
                                event info for Line One after that.

                                Besides theoretical blah-blah, it answers the question about
                                time-consuming operations (like db connections) on events. Nothing
                                terrible will happen as you can see: the system will keep bubbling, but
                                the instance of the original event will wait you however long you want
                                (until the end of the session).


                                2. Event handling
                                Dear Michael, I know very well how to get event properties in a FF/IE
                                compatible way. Still thank you for your long responce.
                                The question was & is: what is the most universal and reliable way if
                                we need the bubbles back-tracking?
                                Consider the code below (instead of div's could be table cells, span's,
                                ul's - just search this newsgroup for samples). So we have some
                                universal update(objectRe f, newContent) function and we want to use it
                                on OuterLayer, and only on it (filtering out the underlaying elements).
                                What update() function should we use?

                                [1]
                                ...."OuterLayer " onclick="update (this,newConten t)"...
                                ....
                                function update(obj, content) {
                                // Doesn't work at all within ECMA:
                                // var trg = (event)? event.srcElemen t : /* what to do with FF? */
                                //if (obj == trg) {
                                // obj.innerHTML = content + obj.innerHTML;
                                //}
                                }

                                [2]
                                ...."OuterLayer " onclick="update (this,newConten t)"...
                                ....
                                function update(obj, content) {
                                /* It works, but ECMA doesn't promise (but neither prohibits) this
                                functionality: */
                                var trg = (event)? event.srcElemen t : obj.onclick.arg uments[0].target;
                                if (obj == trg) {
                                obj.innerHTML = content + obj.innerHTML;
                                }
                                }

                                [3]
                                ....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 */
                                var trg = (tar)? tar : event.srcElemen t;
                                if (obj == trg) {
                                obj.innerHTML = content + obj.innerHTML;
                                }
                                }

                                If I'm writing a library for further distribution, I need to take a
                                decision right here. My update() function can be called from anywhere:
                                from an event handler, from another function, directly. What is the way
                                to make it maximum usage-friendly?

                                <html>
                                <head>
                                <title>Untitl ed Document</title>
                                <meta http-equiv="Content-Type" content="text/html;
                                charset=iso-8859-1">
                                <script>

                                </script>
                                <body bgcolor="#FFFFF F">
                                <div id="OuterLayer " onclick="<!-- do what ? -->"
                                style="position :absolute; width:400px; height:400px;
                                background-color:#FFFF99; z-index:1; left: 50px; top: 50px">
                                <div id="InnerLayer1 " style="position :absolute; width:300px;
                                height:300px; background-color:#66FF66; z-index:2; left: 50px; top:
                                50px">
                                <div id="InnerLayer2 " style="position :absolute; width:200px;
                                height:200px; background-color:#CCCCFF; z-index:3; left: 50px; top:
                                50px"></div>
                                </div>
                                </div>

                                </body>
                                </html>

                                Comment

                                Working...