[9] DOMイベントにおける対象とは、 イベントがディスパッチされたり、イベントリスナーが呼び出されたりする際の文脈として用いられるオブジェクトです。
disptch at target
fire at target
... のような表現で記述されます。
[23] dispatchEvent メソッドでは、文脈オブジェクトが対象となります。
[24] 仕様書上のディスパッチには、対象上書きという引数があります >>25。
これが指定されていると、通常とは異なるオブジェクトが対象になります。
現在これが用いられているのは、window.open
で発生する Window オブジェクトでの load
イベント (Document が対象)Window オブジェクトでの pageshow
イベント (Document が対象)Window オブジェクトでの pagehide
イベント (Document が対象)Window オブジェクトでの unload
イベント (Document が対象)
Event インターフェイス target 属性 (DOM)[13] Event インターフェイスの target
属性は、イベントの対象を返します。
[14] Event インターフェイスの
target IDL属性と
srcElement IDL属性の取得器は、
次のようにしなければなりません >>3。
[63] イベントは、
対象を持ちます
>>3。初期値は null です >>3。
[64] ディスパッチの際に値がディスパッチ対象の
EventTarget に設定されます。
Event インターフェイス currentTarget 属性 (DOM)[15] Event インターフェイスの currentTarget
属性は、イベントの現在の対象を返します。
[16] この属性の初期値は null です >>3。イベントリスナーが呼び出される際に登録元の
EventTarget に設定されます。
[17] 親がない EventTarget なら普通 target と
currentTarget は一致しますが、DOM木中の節点など親子関係があると、
捕獲や bubble が発生するので、等しくないことがあります。
EventTarget インターフェイス (DOM)[11] DOMイベントの対象となり得るオブジェクトは、すべて EventTarget
インターフェイスを継承しています。
EventTarget であることは
DOM Standard 上明示的には要求されていないようですが、 Event
インターフェイスの currentTarget が EventTarget
型となっているので、そうでなければ要件を満たせません。[44] EventTarget コンストラクターは、次のようにしなければなりません
>>3。
EventTarget を返します。[7] 次の状態を持ちます。
EventTarget オブジェクトの種別ごとに定義されています。[8] 著者は、 EventTarget のメソッドを通してイベントリスナーリストに含まれる個々のイベントリスナーを追加したり削除したりできます。
[33] イベントリスナーリストに対する演算は、イベントリスナーを参照。
EventTarget の一覧[27] 次のインターフェイスは EventTarget を継承しています
(直接継承しているもののみ)。Node DOM StandardWindow HTML StandardWorkerGlobalScope HTML StandardTextTrack HTML StandardTextTrackCue HTML StandardAudioTrackList HTML StandardVideoTrackList HTML StandardTextTrackList HTML StandardNotification Notifications API StandardApplicationCache HTML StandardEventSource HTML StandardWebSocket HTML StandardMessagePortBroadcastChannelWorker HTML StandardSharedWorker HTML StandardXMLHttpRequestEventTarget (XMLHttpRequest, XMLHttpRequestUpload) XHRRTCPeerConnection WebRTCRTCDataChannel WebRTCIDBRequest IndexedDBIDBDatabase IndexedDBIDBTransaction IndexedDBPerformanceMediaSourceSourceBufferSourceBufferListServiceWorkerContainer
[46] 他に、 EventTarget コンストラクターで作成された素の
EventTarget オブジェクトがの
DOM Standard の改訂で追加されました。著者がサブクラス化して任意の
JavaScript オブジェクトに使うことが想定されています。
[18] エラーの報告においては、 error イベントがディスパッチされるオブジェクトが対象と呼ばれています。
[4] DOM2 や DOM3 の頃は EventTarget は他のインターフェイスと継承関係になく、
言語束縛依存のキャストによって取得できるとされていました。 JavaScript
の場合は関係するオブジェクトに EventTarget
のメソッドも生えている形になっていました。 (実装上は Node
などの親クラスとなっていたようです。)
[5] WebIDL の時代になって implements によって IDL
中にこれを記述できるようになりました。
[6] 更に DOM Standard により Node などが継承する親インターフェイスとして記述されるようになりました
>>2。
[37] on() / off() sketch. ( 版) https://gist.github.com/annevk/5238964
[38] Fix the incorrect side effects of dispatch' target override · whatwg/dom@b8a8eb2 ( 版) https://github.com/whatwg/dom/commit/b8a8eb272d1ff3c654b3c5ae690c990f1d0cd700
[40] Redesign fire an event and target override (annevk著, ) https://github.com/whatwg/dom/commit/e19d7eef74fb83792415a4a78ed866d28f4cc543
[42] Editorial: major cleanup around firing and dispatching events (annevk著, ) https://github.com/whatwg/html/commit/8ffbd146d4709057e45dd6cfc81e7a70897050c5
[43] Allow constructing and subclassing EventTarget (domenic著, ) https://github.com/whatwg/dom/commit/c4c1c8b47340a1e5ecc1a07670927b831f240586
[47] 16487 – EventTarget constructor () https://www.w3.org/Bugs/Public/show_bug.cgi?id=16487
[48] Make EventTarget subclassable · Issue #441 · whatwg/dom () https://github.com/whatwg/dom/issues/441
[49] Allow constructing and subclassing EventTarget by domenic · Pull Request #467 · whatwg/dom () https://github.com/whatwg/dom/pull/467
[50] Expose Event and EventTarget to AudioWorklet (hoch著, ) https://github.com/whatwg/dom/commit/601655d48abe3979200e42cefad60cdf0d752693
[51] [audioworklet] Ensure MessagePort is exposed in audio worklets · Issue #1266 · WebAudio/web-audio-api () https://github.com/WebAudio/web-audio-api/issues/1266
[52] Expose Event, EventTarget to AudioWorklet · Issue #515 · whatwg/dom () https://github.com/whatwg/dom/issues/515
[53] Fix 515: Expose Event, EventTarget to AudioWorklet by hoch · Pull Request #516 · whatwg/dom () https://github.com/whatwg/dom/pull/516
[54] Clear event's target and relatedTarget when dispatch ends at a shadow… (TakayoshiKochi著, ) https://github.com/whatwg/dom/commit/9ada239e14fe7964577276ef9d8972069e665247
[55] Event.target should return null after dispatch is complete, when stopped at ShadowRoot · Issue #511 · whatwg/dom () https://github.com/whatwg/dom/issues/511
[56] Set event.target to null when dispatching finishes at ShadowRoot by TakayoshiKochi · Pull Request #513 · whatwg/dom () https://github.com/whatwg/dom/pull/513
[57] Explain why OffscreenCanvas is an EventTarget (GloverDonovan著, ) https://github.com/whatwg/html/commit/eb2d54687d0e9fa9e9f70d107fc07663c17c73af
[58] Explain why OffscreenCanvas inherits EventTarget by GloverDonovan · Pull Request #3474 · whatwg/html () https://github.com/whatwg/html/pull/3474
[59] Editorial: add "add an event listener" hook (annevk著, ) https://github.com/whatwg/dom/commit/2bdabb15de8112d9783cf393a3d6c37911149f77
[61] Editorial: add "add an event listener" hook by annevk · Pull Request #596 · whatwg/dom () https://github.com/whatwg/dom/pull/596
[62] Editorial: create a concept for event's target (annevk著, ) https://github.com/whatwg/dom/commit/07c110c87b8e4b1f7ecae09727b859bb75b17a73
[66] Define a target concept that Event.target and Event.srcElement reflect · Issue #570 · whatwg/dom () https://github.com/whatwg/dom/issues/570
[67] Editorial: create a concept for event's target by annevk · Pull Request #621 · whatwg/dom () https://github.com/whatwg/dom/pull/621
[68] Define Event's srcElement and returnValue (miketaylr著, ) https://github.com/whatwg/dom/commit/e36b369a418663603e081d211dc844d197d97ecb
[34] Align with IDL constructor changes (autokagami著, ) https://github.com/whatwg/dom/commit/57512fac17cf2f1c4c85be4aec178c8086ee5ee4
DocumentではなくWindowで発火されるわけですから、Documentのイベントリスナーは呼び出されず、Windowのイベントリスナーだけが呼び出されます。 更には、 本来ならevent.targetもevent.currentTargetもWindowになるはずなのですが、event.targetはなぜか上書きされてDocumentになるのです。