Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #24465
| Message-ID | <5294499.JCWY7Ix9cy@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2014-05-29 22:35 +0200 |
| Subject | Re: Event target in browsers |
| Newsgroups | comp.lang.javascript |
| References | <69413$5387765f$6def49ce$18425@nntpswitch.blueworldhosting.com> |
Cezary Tomczyk wrote:
> <button id="btn" type="submit" name="example" value="0">
> <img id="image" alt="Feed icon" […] />
> </button>
> […]
> var el = document.getElementById('btn') […];
>
> function getEventTarget(e) {
> var target = e.target;
> if (target) {
> if (target.nodeType !== 1) {
> target = target.parentNode;
> }
> } else {
> target = e.srcElement;
> }
> return target;
> }
>
> function getElementNodeName(el) {
> var nn = 'unknown';
>
> if (el.tagName) {
> nn = el.tagName;
> } else if (el.nodeName) {
> nn = el.nodeName;
> }
>
> return nn.toLowerCase();
> }
>
> function listener(e) {
> var tar = getEventTarget(e);
> if (getElementNodeName(tar) === 'img') {
> tar = tar.parentNode;
> }
> […]
> }
>
> el.addEventListener('click', listener, false);
> […]
> In Firefox 29.0.1 and IE 11 I am getting as a target HTML "button"
> element, but in Chrome 35.0.1916.114 m I am getting HTML "img" element.
> […]
> My understand is because different browsers implementing different way
> how the events works.
> http://www.w3.org/wiki/Handling_events_with_JavaScript#How_events_work
>
> Is my approach correct?
No. The “click” event bubbles in all DOM implementations, and
getElementNodeName() is overkill.
getEventTarget() does not work because where only “srcElement” is available,
the reference to the event object is _not_ passed as first argument to the
listener (IE/MSHTML < 9 or Compatibility Mode in IE 9), and “window.event”
must be accessed instead. But since EventTarget::addEventListener() is not
implemented in the latter case, and you would not want to augment host
objects, getEventTarget() is superfluous.
As you are relying on the W3C DOM API already, just use “currentTarget”
instead of “target”. (Works in Chromium 34. I need NSAPI plugin support,
so I cannot test in Chromium 35 for the time being.)
<http://www.w3.org/TR/2014/CR-dom-20140508/#interface-event>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Event target in browsers Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-05-29 20:03 +0200
Re: Event target in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-29 22:35 +0200
Re: Event target in browsers Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-30 13:26 +0200
Re: Event target in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-30 14:31 +0200
Re: Event target in browsers Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-30 17:50 +0200
Re: Event target in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-01 22:19 +0200
Re: Event target in browsers Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-05-30 20:04 +0200
Re: Event target in browsers Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-30 20:22 +0200
Re: Event target in browsers Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-05-30 20:43 +0200
Re: Event target in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-30 21:47 +0200
Re: Event target in browsers Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-30 22:29 +0200
Re: Event target in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-31 00:07 +0200
Re: Event target in browsers Christoph Michael Becker <cmbecker69@arcor.de> - 2014-06-02 14:53 +0200
Re: Event target in browsers Christoph Michael Becker <cmbecker69@arcor.de> - 2014-06-02 21:41 +0200
Re: Event target in browsers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-30 21:38 +0200
csiph-web