window.event.srcElement

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

    #1

    window.event.srcElement

    I have this IE specific bit of code for finding the originating node:

    var obj=window.even t.srcElement;

    How do I do that cross browser (Opera, NS, Safari...)? Is there a standard
    DOM method?

    I seem to recall NS worked something like this:

    onmousedown=han dleThat;

    function handleThat(e){
    e.target;
    // but then I'd have to take the parent to get the same node as IE
    }

    Event handling makes my head spin!

    Jeff


  • mscir

    #2
    Re: window.event.sr cElement

    Jeff Thies wrote:[color=blue]
    > I have this IE specific bit of code for finding the originating node:
    >
    > var obj=window.even t.srcElement;
    >
    > How do I do that cross browser (Opera, NS, Safari...)? Is there a standard
    > DOM method?
    >
    > I seem to recall NS worked something like this:
    >
    > onmousedown=han dleThat;
    >
    > function handleThat(e){
    > e.target;
    > // but then I'd have to take the parent to get the same node as IE
    > }
    >
    > Event handling makes my head spin!
    > Jeff[/color]

    Looks like you've got a good memory, I think this page deals with it:

    http://www.sitepoint.com/article/scr...y-javascript/3

    // in a cross-browser fashion
    if (window.event) {
    target = window.event.sr cElement;
    } else if (e) {
    target = e.target;
    } else return;

    Mike

    Comment

    • Martin Honnen

      #3
      Re: window.event.sr cElement



      Jeff Thies wrote:
      [color=blue]
      > I have this IE specific bit of code for finding the originating node:
      >
      > var obj=window.even t.srcElement;
      >
      > How do I do that cross browser (Opera, NS, Safari...)? Is there a standard
      > DOM method?
      >
      > I seem to recall NS worked something like this:
      >
      > onmousedown=han dleThat;
      >
      > function handleThat(e){
      > e.target;
      > // but then I'd have to take the parent to get the same node as IE
      > }[/color]

      Yes, Mozilla implements the W3C DOM Level 2 Events module and that
      defines properties like event.target with the DOM being node based so
      such a target node can be a text node and not necessarily an element
      node as IE's window.event.sr cElement. Then you need to find the element
      node containing the target node by walking the parentNode hierarchy:

      element.onmouse over = function (evt) {
      var srcElement;
      if (evt && evt.target) {
      srcElement = evt.target;
      while (srcElement.nod eType != 1) {
      srcElement = srcElement.pare ntNode;
      }
      }
      else if (window.event) {
      srcElement = window.event.sr cElement;
      }
      if (srcElement) {
      ...
      }
      }

      I think Opera 7 implements the W3C DOM target property as well but also
      tries to support IE's window.event and window.event.sr cElement.
      --

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

      Comment

      • Michael Winter

        #4
        Re: window.event.sr cElement

        On Mon, 19 Apr 2004 13:19:55 +0200, Martin Honnen <mahotrash@yaho o.de>
        wrote:
        [color=blue]
        >
        >
        > Jeff Thies wrote:
        >[color=green]
        >> I have this IE specific bit of code for finding the originating node:
        >>
        >> var obj=window.even t.srcElement;
        >>
        >> How do I do that cross browser (Opera, NS, Safari...)? Is there a
        >> standard
        >> DOM method?
        >>
        >> I seem to recall NS worked something like this:
        >>
        >> onmousedown=han dleThat;
        >>
        >> function handleThat(e){
        >> e.target;
        >> // but then I'd have to take the parent to get the same node as IE
        >> }[/color]
        >
        > Yes, Mozilla implements the W3C DOM Level 2 Events module and that
        > defines properties like event.target with the DOM being node based so
        > such a target node can be a text node and not necessarily an element
        > node as IE's window.event.sr cElement. Then you need to find the element
        > node containing the target node by walking the parentNode hierarchy:[/color]

        Though both Mozilla and Opera implement addEventListene r() for text nodes,
        they don't seem to honour their events (not thoroughly tested). If a
        listener is placed on an ancestor of the text node, I find that the target
        assigned is the text node's parent. This might have been intended to mimic
        IE's behaviour. However, it would still be wise to check in case a more
        conforming implementation exists.

        [snipped code]

        Depending what the OP is trying to accomplish, you might be able to avoid
        the issue altogether by taking advantage of event bubbling. Consider

        <p>Some text</p>

        If the user clicked on "Some text", the target property of the event
        object might refer to the text node that contained the text, whilst
        srcElement will always refer to the paragraph element. If you wanted to
        alter the paragraph, simply place a listener on it and use the this
        operator to refer directly to the element. Similarly

        <table>
        <tr>
        <td>Some text</td>

        listeners placed on the cell, row, or containing table elements will all
        receive events targeted at the text node, but the this operator will
        always refer to the element that the listener was registered with[1].

        I think the only reason to explicitly check the original target is to
        filter events when a listener can be fired from multiple sources
        (descendants).
        [color=blue]
        > I think Opera 7 implements the W3C DOM target property as well but also
        > tries to support IE's window.event and window.event.sr cElement.[/color]

        You're right; it does.

        Mike


        [1] Be aware that IE contains a bug where the this operator isn't set
        correctly when a listener is fired after being added with attachEvent(). A
        workaround is to avoid attachEvent() and detachEvent() altogether and use
        the event properties. Another is to set the this operator yourself:

        if(Function.pro totype){
        Function.protot ype.evtCall=Fun ction.prototype .call ?
        function(o,p){r eturn this.call(o,p); }:
        function(o,p){v ar n='__eC';while( o[n]){n+=n;}
        o[n]=this;var r=o[n](p);delete o[n];return r;};
        }
        function attachEvent(o,t ,l){
        if(o&&l&&o.atta chEvent){
        o.attachEvent(t ,function(){ret urn l.evtCall(o,eve nt);});
        }
        }

        Notice that listeners will be passed the event object automatically,
        rather than having to obtain it during execution (optional).

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: window.event.sr cElement

          "Jeff Thies" <nospam@nospam. net> writes:
          [color=blue]
          > I have this IE specific bit of code for finding the originating node:
          >
          > var obj=window.even t.srcElement;
          >
          > How do I do that cross browser (Opera, NS, Safari...)? Is there a standard
          > DOM method?[/color]

          Yes.
          First of all, you need the event. In standard compliant browsers, the
          event object is not (necessarily) reachable through a global variable,
          but is passed as the argument of the event handler function.

          Then you can use the "target" property of the event.

          What I ususally do is:
          ---
          function somehandler(eve nt) {
          event = event || window.event; // IE sucks
          var tgt = event.tgt || event.srcElemen t; // IE sucks
          ...
          }
          ---
          (Yes, I usually include the comments :)
          [color=blue]
          > I seem to recall NS worked something like this:
          >
          > onmousedown=han dleThat;
          >
          > function handleThat(e){
          > e.target;
          > // but then I'd have to take the parent to get the same node as IE[/color]

          No, it is the same node as IE's window.event.sr cElement (except
          perhaps during bubbling).

          /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

          • Martin Honnen

            #6
            Re: window.event.sr cElement



            Michael Winter wrote:
            [color=blue]
            > On Mon, 19 Apr 2004 13:19:55 +0200, Martin Honnen <mahotrash@yaho o.de>
            > wrote:
            >[color=green]
            >>
            >>
            >> Jeff Thies wrote:
            >>[color=darkred]
            >>> I have this IE specific bit of code for finding the originating node:
            >>>
            >>> var obj=window.even t.srcElement;
            >>>
            >>> How do I do that cross browser (Opera, NS, Safari...)? Is there a
            >>> standard
            >>> DOM method?
            >>>
            >>> I seem to recall NS worked something like this:
            >>>
            >>> onmousedown=han dleThat;
            >>>
            >>> function handleThat(e){
            >>> e.target;
            >>> // but then I'd have to take the parent to get the same node as IE
            >>> }[/color]
            >>
            >>
            >> Yes, Mozilla implements the W3C DOM Level 2 Events module and that
            >> defines properties like event.target with the DOM being node based
            >> so such a target node can be a text node and not necessarily an
            >> element node as IE's window.event.sr cElement. Then you need to find
            >> the element node containing the target node by walking the parentNode
            >> hierarchy:[/color]
            >
            >
            > Though both Mozilla and Opera implement addEventListene r() for text
            > nodes, they don't seem to honour their events (not thoroughly tested).
            > If a listener is placed on an ancestor of the text node, I find that the
            > target assigned is the text node's parent. This might have been intended
            > to mimic IE's behaviour. However, it would still be wise to check in
            > case a more conforming implementation exists.[/color]

            Actually Mozilla intially fired mouse events with text nodes as the
            target in an attempt to implement the W3C DOM correctly (at least it was
            described as such) however somewhere after Mozilla 1.0 a Mozilla
            developer considering text nodes as target to be akward changed the
            event handling code and managed to get the changes into the Mozilla
            (with Mozilla 1.3 or 1.4) so now you have to live with different
            versions having different event target nodes.
            The text node is still reported as the explicitOrigina lTarget:

            <html>
            <head>
            <title>event.ta rget/event.originalT arget</title>
            <script type="text/javascript">
            function setEventListene r () {
            var p;
            if (document.getEl ementById) {
            p = document.getEle mentById('aP');
            if (p && p.addEventListe ner) {
            p.addEventListe ner('click',
            function (evt) {
            alert(evt.type + ' on ' + evt.target + ' with
            explicitOrigina lTarget ' + evt.explicitOri ginalTarget);
            },
            false
            );
            }
            }
            }

            window.onload = function (evt) {
            setEventListene r();
            };
            </script>
            </head>
            <body>
            <p id="aP">
            Kibology for all. Click for test.
            </p>
            </body>
            </html>


            --

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

            Comment

            Working...