On Mon, 19 Apr 2004 13:19:55 +0200, Martin Honnen <mahotrash@yahoo.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.event.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=handleThat;
>>
>> 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.srcElement. Then you need to find the element
> node containing the target node by walking the parentNode hierarchy:[/color]
Though both Mozilla and Opera implement addEventListener() 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.srcElement.[/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.prototype){
Function.prototype.evtCall=Function.prototype.call ?
function(o,p){return this.call(o,p);}:
function(o,p){var 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.attachEvent){
o.attachEvent(t,function(){return l.evtCall(o,event);});
}
}
Notice that listeners will be passed the event object automatically,
rather than having to obtain it during execution (optional).
--
Michael Winter
M.Winter@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)