forked from w3c/dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom-core.html
9281 lines (7522 loc) · 539 KB
/
dom-core.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html><html lang="en-US"><meta charset="utf-8">
<title>DOM Standard</title>
<link href="http://www.whatwg.org/style/specification" rel="stylesheet">
<link href="//resources.whatwg.org/logo-dom.svg" rel="icon">
<div class="head">
<p><a class="logo" href="//www.whatwg.org/"><img alt="WHATWG" height="100" src="//resources.whatwg.org/logo-dom.svg" width="100"></a></p>
<h1 class="allcaps">DOM</h1>
<h2 class="no-num no-toc" id="living-standard-—-last-updated-20-march-2014">Living Standard — Last Updated 20 March 2014</h2>
https://www.w3.org/Bugs/Public/show_bug.cgi?id=25086
<dl>
<dt>This Version:
<dd><a href="http://dom.spec.whatwg.org/">http://dom.spec.whatwg.org/</a>
<dt>Participate:</dt>
<dd class="dontpublish">Send feedback to
<a href="mailto:[email protected]?subject=DOM%3A%20">[email protected]</a> (<a href="http://lists.w3.org/Archives/Public/www-dom/">archives</a>) or
<a href="https://www.w3.org/Bugs/Public/enter_bug.cgi?product=WebAppsWG&component=DOM">file a bug</a>
(<a href="https://www.w3.org/Bugs/Public/buglist.cgi?product=WebAppsWG&component=DOM&resolution=---">open bugs</a>)
<dd class="dontpublish"><a href="http://wiki.whatwg.org/wiki/IRC">IRC: #whatwg on Freenode</a>
<dt>Version History:
<dd><a href="https://github.com/whatwg/dom/commits">https://github.com/whatwg/dom/commits</a>
<dd><a href="https://twitter.com/thedomstandard">@thedomstandard</a>
<dt>Editors:
<dd><a href="http://annevankesteren.nl/">Anne van Kesteren</a>
(<a href="http://www.mozilla.org/">Mozilla</a>)
<<a href="mailto:[email protected]">[email protected]</a>>
<dd>Aryeh Gregor
(<a href="http://www.mozilla.org/">Mozilla</a>)
<<a href="mailto:[email protected]">[email protected]</a>>
<dd>Ms2ger
(<a href="http://www.mozilla.org/">Mozilla</a>)
<<a href="mailto:[email protected]">[email protected]</a>>
</dl>
<script async="" src="//resources.whatwg.org/file-bug.js"></script>
<p class="copyright"><a href="http://creativecommons.org/publicdomain/zero/1.0/" rel="license"><img alt="CC0" src="http://i.creativecommons.org/p/zero/1.0/80x15.png"></a>
To the extent possible under law, the editors have waived all copyright and
related or neighboring rights to this work. In addition, as of
20 March 2014, the editors have made this specification available
https://www.w3.org/Bugs/Public/show_bug.cgi?id=25086
under the
<a href="http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0" rel="license">Open Web Foundation Agreement Version 1.0</a>,
which is available at
http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0.
</div>
<h2 class="no-num no-toc" id="abstract">Abstract</h2>
<p>DOM defines a platform-neutral model for events and node trees.
<h2 class="no-num no-toc" id="table-of-contents">Table of Contents</h2>
<!--begin-toc-->
<ol class="toc">
<li><a class="no-num" href="#goals">Goals</a></li>
<li><a href="#conformance"><span class="secno">1 </span>Conformance</a>
<ol>
<li><a href="#dependencies"><span class="secno">1.1 </span>Dependencies</a></li>
<li><a href="#extensibility"><span class="secno">1.2 </span>Extensibility</a></ol></li>
<li><a href="#terminology"><span class="secno">2 </span>Terminology</a>
<ol>
<li><a href="#trees"><span class="secno">2.1 </span>Trees</a></li>
<li><a href="#strings"><span class="secno">2.2 </span>Strings</a></li>
<li><a href="#ordered-sets"><span class="secno">2.3 </span>Ordered sets</a></li>
<li><a href="#namespaces"><span class="secno">2.4 </span>Namespaces</a></ol></li>
<li><a href="#errors"><span class="secno">3 </span>Errors</a>
<ol>
<li><a href="#exception-domexception"><span class="secno">3.1 </span>Exception <code>DOMException</code></a></li>
<li><a href="#interface-domerror"><span class="secno">3.2 </span>Interface <code>DOMError</code></a></li>
<li><a href="#error-names-0"><span class="secno">3.3 </span>Error names</a></ol></li>
<li><a href="#events"><span class="secno">4 </span>Events</a>
<ol>
<li><a href="#introduction-to-dom-events"><span class="secno">4.1 </span>Introduction to "DOM Events"</a></li>
<li><a href="#interface-event"><span class="secno">4.2 </span>Interface <code>Event</code></a></li>
<li><a href="#interface-customevent"><span class="secno">4.3 </span>Interface <code>CustomEvent</code></a></li>
<li><a href="#constructing-events"><span class="secno">4.4 </span>Constructing events</a></li>
<li><a href="#defining-event-interfaces"><span class="secno">4.5 </span>Defining event interfaces</a></li>
<li><a href="#interface-eventtarget"><span class="secno">4.6 </span>Interface <code>EventTarget</code></a></li>
<li><a href="#dispatching-events"><span class="secno">4.7 </span>Dispatching events</a></li>
<li><a href="#firing-events"><span class="secno">4.8 </span>Firing events</a></ol></li>
<li><a href="#nodes"><span class="secno">5 </span>Nodes</a>
<ol>
<li><a href="#introduction-to-the-dom"><span class="secno">5.1 </span>Introduction to "The DOM"</a></li>
<li><a href="#node-tree"><span class="secno">5.2 </span>Node tree</a>
<ol>
<li><a href="#mutation-algorithms"><span class="secno">5.2.1 </span>Mutation algorithms</a></li>
<li><a href="#interface-nonelementparentnode"><span class="secno">5.2.2 </span>Interface <code>NonElementParentNode</code></a></li>
<li><a href="#interface-parentnode"><span class="secno">5.2.3 </span>Interface <code>ParentNode</code></a></li>
<li><a href="#interface-nondocumenttypechildnode"><span class="secno">5.2.4 </span>Interface <code>NonDocumentTypeChildNode</code></a></li>
<li><a href="#interface-childnode"><span class="secno">5.2.5 </span>Interface <code>ChildNode</code></a></li>
<li><a href="#collections:-elements"><span class="secno">5.2.6 </span>Collections: <code>Elements</code></a></li>
<li><a href="#old-style-collections:-nodelist-and-htmlcollection"><span class="secno">5.2.7 </span>Old-style collections: <code>NodeList</code> and <code>HTMLCollection</code></a>
<ol>
<li><a href="#interface-nodelist"><span class="secno">5.2.7.1 </span>Interface <code>NodeList</code></a></li>
<li><a href="#interface-htmlcollection"><span class="secno">5.2.7.2 </span>Interface <code>HTMLCollection</code></a></ol></ol></li>
<li><a href="#mutation-observers"><span class="secno">5.3 </span>Mutation observers</a>
<ol>
<li><a href="#interface-mutationobserver"><span class="secno">5.3.1 </span>Interface <code>MutationObserver</code></a></li>
<li><a href="#queuing-a-mutation-record"><span class="secno">5.3.2 </span>Queuing a mutation record</a></li>
<li><a href="#interface-mutationrecord"><span class="secno">5.3.3 </span>Interface <code>MutationRecord</code></a></li>
<li><a href="#garbage-collection"><span class="secno">5.3.4 </span>Garbage collection</a></ol></li>
<li><a href="#interface-node"><span class="secno">5.4 </span>Interface <code>Node</code></a></li>
<li><a href="#interface-document"><span class="secno">5.5 </span>Interface <code>Document</code></a>
<ol>
<li><a href="#interface-domimplementation"><span class="secno">5.5.1 </span>Interface <code>DOMImplementation</code></a></ol></li>
<li><a href="#interface-documentfragment"><span class="secno">5.6 </span>Interface <code>DocumentFragment</code></a></li>
<li><a href="#interface-documenttype"><span class="secno">5.7 </span>Interface <code>DocumentType</code></a></li>
<li><a href="#interface-element"><span class="secno">5.8 </span>Interface <code>Element</code></a>
<ol>
<li><a href="#interface-attr"><span class="secno">5.8.1 </span>Interface <code>Attr</code></a></ol></li>
<li><a href="#interface-characterdata"><span class="secno">5.9 </span>Interface <code>CharacterData</code></a></li>
<li><a href="#interface-text"><span class="secno">5.10 </span>Interface <code>Text</code></a></li>
<li><a href="#interface-processinginstruction"><span class="secno">5.11 </span>Interface <code>ProcessingInstruction</code></a></li>
<li><a href="#interface-comment"><span class="secno">5.12 </span>Interface <code>Comment</code></a></ol></li>
<li><a href="#ranges"><span class="secno">6 </span>Ranges</a>
<ol>
<li><a href="#introduction-to-dom-ranges"><span class="secno">6.1 </span>Introduction to "DOM Ranges"</a></li>
<li><a href="#interface-range"><span class="secno">6.2 </span>Interface <code>Range</code></a></ol></li>
<li><a href="#traversal"><span class="secno">7 </span>Traversal</a>
<ol>
<li><a href="#interface-nodeiterator"><span class="secno">7.1 </span>Interface <code>NodeIterator</code></a></li>
<li><a href="#interface-treewalker"><span class="secno">7.2 </span>Interface <code>TreeWalker</code></a></li>
<li><a href="#interface-nodefilter"><span class="secno">7.3 </span>Interface <code>NodeFilter</code></a></ol></li>
<li><a href="#sets"><span class="secno">8 </span>Sets</a>
<ol>
<li><a href="#interface-domtokenlist"><span class="secno">8.1 </span>Interface <code>DOMTokenList</code></a></li>
<li><a href="#interface-domsettabletokenlist"><span class="secno">8.2 </span>Interface <code>DOMSettableTokenList</code></a></ol></li>
<li><a href="#historical"><span class="secno">9 </span>Historical</a>
<ol>
<li><a href="#dom-events"><span class="secno">9.1 </span>DOM Events</a></li>
<li><a href="#dom-core"><span class="secno">9.2 </span>DOM Core</a></li>
<li><a href="#dom-ranges"><span class="secno">9.3 </span>DOM Ranges</a></li>
<li><a href="#dom-traversal"><span class="secno">9.4 </span>DOM Traversal</a></ol></li>
<li><a class="no-num" href="#references">References</a></li>
<li><a class="no-num" href="#acknowledgments">Acknowledgments</a></ol>
<!--end-toc-->
<h2 class="no-num" id="goals">Goals</h2>
<p>This specification standardizes the DOM. It does so as follows:</p>
<ol>
<li>
<p>By consolidating <cite>DOM Level 3 Core</cite>
<a class="informative" href="#refsDOM3CORE">[DOM3CORE]</a>,
<cite>Element Traversal</cite>
<a class="informative" href="#refsELEMENTTRAVERSAL">[ELEMENTTRAVERSAL]</a>,
<cite>Selectors API Level 2</cite>
<a class="informative" href="#refsSELECTORSAPI">[SELECTORSAPI]</a>, the
"DOM Event Architecture" and "Basic Event Interfaces" chapters of
<cite>DOM Level 3 Events</cite>
<a class="informative" href="#refsDOM3EVENTS">[DOM3EVENTS]</a> (specific type
of events do not belong in the base specification), and
<cite>DOM Level 2 Traversal and Range</cite>
<a class="informative" href="#refsDOM2TR">[DOM2TR]</a>, and:
<ul>
<li>Aligning them with the needs of JavaScript where possible.
<li>Aligning them with existing implementations.
<li>Simplifying them as much as possible.
</ul>
<li><p>By moving features from the HTML standard that ought to be part of the DOM
platform here. <a href="#refsHTML">[HTML]</a>
<li>
<p>By defining a replacement for the "Mutation Events" and
"Mutation Name Event Types" chapters of <cite>DOM Level 3 Events</cite>
<a class="informative" href="#refsDOM3EVENTS">[DOM3EVENTS]</a> as the old model
was problematic.
<p class="note">The old model is expected to be removed from implementations
in due course.
<li><p>By defining new features that simplify common DOM operations.
</ol>
<h2 id="conformance"><span class="secno">1 </span>Conformance</h2>
<p>All diagrams, examples, and notes in this specification are
non-normative, as are all sections explicitly marked non-normative.
Everything else in this specification is normative.
<p>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and
"OPTIONAL" in this document are to be interpreted as described in RFC 2119.
For readability, these words do not appear in all uppercase letters in this
specification. <a href="#refsRFC2119">[RFC2119]</a>
<p>Requirements phrased in the imperative as part of algorithms
(such as "strip any leading space characters" or "return false and
terminate these steps") are to be interpreted with the meaning of the
key word ("must", "should", "may", etc) used in introducing the
algorithm.
<p>Conformance requirements phrased as algorithms or specific steps
may be implemented in any manner, so long as the end result is
equivalent. (In particular, the algorithms defined in this
specification are intended to be easy to follow, and not intended to
be performant.)
<p id="hardwareLimitations">User agents may impose
implementation-specific limits on otherwise unconstrained inputs,
e.g. to prevent denial of service attacks, to guard against running
out of memory, or to work around platform-specific limitations.
<p>When a method or an attribute is said to call another method or attribute, the user agent must invoke its internal API for that attribute or method so that e.g. the author can't change the behavior by overriding attributes or methods with custom properties or functions in JavaScript.
<p>Unless otherwise stated, string comparisons are done in a <a href="#case-sensitive">case-sensitive</a> manner.
<h3 id="dependencies"><span class="secno">1.1 </span>Dependencies</h3>
<p>The IDL fragments in this specification must be interpreted as
required for conforming IDL fragments, as described in the Web IDL
specification. <a href="#refsWEBIDL">[WEBIDL]</a>
<p>Some of the terms used in this specification are defined in
<cite>Encoding</cite>, <cite>Selectors</cite>, <cite>Web IDL</cite>, <cite>XML</cite>, and
<cite>Namespaces in XML</cite>.
<a href="#refsENCODING">[ENCODING]</a>
<a href="#refsSELECTORS">[SELECTORS]</a>
<a href="#refsWEBIDL">[WEBIDL]</a>
<a href="#refsXML">[XML]</a>
<a href="#refsXMLNS">[XMLNS]</a>
<h3 id="extensibility"><span class="secno">1.2 </span>Extensibility</h3>
<p>Vendor-specific proprietary extensions to this specification are
strongly discouraged. Authors must not use such extensions, as
doing so reduces interoperability and fragments the user base,
allowing only users of specific user agents to access the content in
question.
<p>If vendor-specific extensions are needed, the members should be
prefixed by vendor-specific strings to prevent clashes with future
versions of this specification. Extensions must be defined so that
the use of extensions neither contradicts nor causes the
non-conformance of functionality defined in the specification.
<!-- thanks to QA Framework -->
<p>When vendor-neutral extensions to this specification are needed,
either this specification can be updated accordingly, or an
extension specification can be written that overrides the
requirements in this specification. When someone applying this
specification to their activities decides that they will recognize
the requirements of such an extension specification, it becomes an
<dfn id="other-applicable-specifications" title="other applicable specifications">applicable
specification</dfn> for the purposes of conformance requirements in
this specification.
<!-- http://www.w3.org/mid/[email protected] -->
<h2 id="terminology"><span class="secno">2 </span>Terminology</h2>
<p>The term <dfn id="context-object">context object</dfn> means the object on which the algorithm,
attribute getter, attribute setter, or method being discussed was called. When the
<a href="#context-object">context object</a> is unambiguous, the term can be omitted.
<!-- XXX we should prolly explain that "set attribute X to Y" works even for
readonly attributes when it is language for implementors -->
<h3 id="trees"><span class="secno">2.1 </span>Trees</h3> <!-- Sorry reddit, this is not /r/trees -->
<p>A <dfn id="concept-tree" title="concept-tree">tree</dfn> is a finite hierarchical tree structure. In
<dfn id="concept-tree-order" title="concept-tree-order">tree order</dfn> is preorder, depth-first
traversal of a <a href="#concept-tree" title="concept-tree">tree</a>.
<!-- http://en.wikipedia.org/wiki/Tree_traversal#Depth-first_Traversal -->
<p>An object that <dfn id="concept-tree-participate" title="concept-tree-participate">participates</dfn> in
a <a href="#concept-tree" title="concept-tree">tree</a> has a
<dfn id="concept-tree-parent" title="concept-tree-parent">parent</dfn>, which is either another object
or null, and an ordered list of zero or more
<dfn id="concept-tree-child" title="concept-tree-child">child</dfn> objects. An object <var title="">A</var> whose
<a href="#concept-tree-parent" title="concept-tree-parent">parent</a> is object <var title="">B</var> is a
<a href="#concept-tree-child" title="concept-tree-child">child</a> of <var title="">B</var>.
<p>The <dfn id="concept-tree-root" title="concept-tree-root">root</dfn> of an object is itself, if its
<a href="#concept-tree-parent" title="concept-tree-parent">parent</a> is null, or else it is the
<a href="#concept-tree-root" title="concept-tree-root">root</a> of its
<a href="#concept-tree-parent" title="concept-tree-parent">parent</a>.
<p>An object <var title="">A</var> is called a
<dfn id="concept-tree-descendant" title="concept-tree-descendant">descendant</dfn> of an object
<var title="">B</var>, if either <var title="">A</var> is a
<a href="#concept-tree-child" title="concept-tree-child">child</a> of <var title="">B</var> or
<var title="">A</var> is a <a href="#concept-tree-child" title="concept-tree-child">child</a> of an
object <var title="">C</var> that is a
<a href="#concept-tree-descendant" title="concept-tree-descendant">descendant</a> of <var title="">B</var>.
<p>An
<dfn id="concept-tree-inclusive-descendant" title="concept-tree-inclusive-descendant">inclusive descendant</dfn> is
an object or one of its
<a href="#concept-tree-descendant" title="concept-tree-descendant">descendants</a>.
<p>An object <var title="">A</var> is called an
<dfn id="concept-tree-ancestor" title="concept-tree-ancestor">ancestor</dfn> of an object
<var title="">B</var> if and only if <var title="">B</var> is a
<a href="#concept-tree-descendant" title="concept-tree-descendant">descendant</a> of
<var title="">A</var>.
<p>An <dfn id="concept-tree-inclusive-ancestor" title="concept-tree-inclusive-ancestor">inclusive ancestor</dfn> is
an object or one of its <a href="#concept-tree-ancestor" title="concept-tree-ancestor">ancestors</a>.
<p>An object <var title="">A</var> is called a
<dfn id="concept-tree-sibling" title="concept-tree-sibling">sibling</dfn> of an object
<var title="">B</var>, if and only if <var title="">B</var> and <var title="">A</var>
share the same non-null <a href="#concept-tree-parent" title="concept-tree-parent">parent</a>.
<p>An object <var title="">A</var> is
<dfn id="concept-tree-preceding" title="concept-tree-preceding">preceding</dfn> an object
<var title="">B</var> if <var title="">A</var> and <var title="">B</var> are in the
same <a href="#concept-tree" title="concept-tree">tree</a> and <var title="">A</var> comes
before <var title="">B</var> in
<a href="#concept-tree-order" title="concept-tree-order">tree order</a>.
<p>An object <var title="">A</var> is
<dfn id="concept-tree-following" title="concept-tree-following">following</dfn> an object
<var title="">B</var> if <var title="">A</var> and <var title="">B</var> are in the
same <a href="#concept-tree" title="concept-tree">tree</a> and <var title="">A</var> comes
after <var title="">B</var> in
<a href="#concept-tree-order" title="concept-tree-order">tree order</a>.
<p>The <dfn id="concept-tree-first-child" title="concept-tree-first-child">first child</dfn> of an object is
its first <a href="#concept-tree-child" title="concept-tree-child">child</a> or null if it has no
<a href="#concept-tree-child" title="concept-tree-child">child</a>.
<p>The <dfn id="concept-tree-last-child" title="concept-tree-last-child">last child</dfn> of an object is
its last <a href="#concept-tree-child" title="concept-tree-child">child</a> or null if it has no
<a href="#concept-tree-child" title="concept-tree-child">child</a>.
<p>The <dfn id="concept-tree-previous-sibling" title="concept-tree-previous-sibling">previous sibling</dfn> of an
object is its first <a href="#concept-tree-preceding" title="concept-tree-preceding">preceding</a>
<a href="#concept-tree-sibling" title="concept-tree-sibling">sibling</a> or null if it has no
<a href="#concept-tree-preceding" title="concept-tree-preceding">preceding</a>
<a href="#concept-tree-sibling" title="concept-tree-sibling">sibling</a>.
<p>The <dfn id="concept-tree-next-sibling" title="concept-tree-next-sibling">next sibling</dfn> of an
object is its first <a href="#concept-tree-following" title="concept-tree-following">following</a>
<a href="#concept-tree-sibling" title="concept-tree-sibling">sibling</a> or null if it has no
<a href="#concept-tree-following" title="concept-tree-following">following</a>
<a href="#concept-tree-sibling" title="concept-tree-sibling">sibling</a>.
<p>The <dfn id="concept-tree-index" title="concept-tree-index">index</dfn> of an object is its number
of <a href="#concept-tree-preceding" title="concept-tree-preceding">preceding</a>
<a href="#concept-tree-sibling" title="concept-tree-sibling">siblings</a>.
<h3 id="strings"><span class="secno">2.2 </span>Strings</h3>
<p>Comparing two strings in a <dfn id="case-sensitive">case-sensitive</dfn> manner means comparing them exactly, code point for code point.
<p>Comparing two strings in a <dfn id="ascii-case-insensitive">ASCII case-insensitive</dfn> manner means comparing them exactly, code point for code point, except that the characters in the range U+0041 .. U+005A (i.e. LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z) and the corresponding characters in the range U+0061 .. U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL LETTER Z) are considered to also match.
<p><dfn id="converted-to-ascii-uppercase" title="converted to ascii uppercase">Converting a string to ASCII uppercase</dfn> means replacing all characters in the range U+0061 to U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL LETTER Z) with the corresponding characters in the range U+0041 to U+005A (i.e. LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z).
<p><dfn id="converted-to-ascii-lowercase" title="converted to ascii lowercase">Converting a string to ASCII lowercase</dfn> means replacing all characters in the range U+0041 to U+005A (i.e. LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z) with the corresponding characters in the range U+0061 to U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL LETTER Z).
<p>A string <var title="">pattern</var> is a <dfn id="prefix-match">prefix match</dfn> for a string
<var title="">s</var> when <var title="">pattern</var> is not longer than
<var title="">s</var> and truncating <var title="">s</var> to
<var title="">pattern</var>'s length leaves the two strings as matches of each
other.
<h3 id="ordered-sets"><span class="secno">2.3 </span>Ordered sets</h3>
<p>The <dfn id="concept-ordered-set-parser" title="concept-ordered-set-parser">ordered set parser</dfn> takes a string
<var title="">input</var> and then runs these steps:
<ol>
<li><p>Let <var title="">position</var> be a pointer into <var title="">input</var>,
initially pointing at the start of the string.
<li><p>Let <var title="">tokens</var> be an ordered set of tokens, initially empty.
<li><p><a href="#skip-ascii-whitespace">Skip ASCII whitespace</a>.
<li><p>While <var title="">position</var> is not past the end of
<var title="">input</var>:
<ol>
<li><p><a href="#collect-a-code-point-sequence">Collect a code point sequence</a> of code points that are
not <a class="external" data-anolis-spec="encoding" href="http://encoding.spec.whatwg.org/#ascii-whitespace">ASCII whitespace</a>.
<li><p>If the collected string is not in <var title="">tokens</var>, append the
collected string to <var title="">tokens</var>.
<li><p><a href="#skip-ascii-whitespace">Skip ASCII whitespace</a>.
</ol>
<li><p>Return <var title="">tokens</var>.
</ol>
<p>To <dfn id="collect-a-code-point-sequence">collect a code point sequence</dfn> of
<var title="">code points</var>, run these steps:
<ol>
<li><p>Let <var title="">input</var> and <var title="">position</var> be the same
variables as those of the same name in the algorithm that invoked these
steps.
<li><p>Let <var title="">result</var> be the empty string.
<li><p>While <var title="">position</var> does not point past the end of
<var title="">input</var> and the code point at <var title="">position</var> is
one of <var title="">code points</var>, append that code point to the end
of <var title="">result</var> and advance <var title="">position</var> to the
next code point in <var title="">input</var>.
<li><p>Return <var title="">result</var>.
</ol>
<p>To <dfn id="skip-ascii-whitespace">skip ASCII whitespace</dfn> means to
<a href="#collect-a-code-point-sequence">collect a code point sequence</a> of
<a class="external" data-anolis-spec="encoding" href="http://encoding.spec.whatwg.org/#ascii-whitespace">ASCII whitespace</a> and discard the
return value.
<p>The <dfn id="concept-ordered-set-serializer" title="concept-ordered-set-serializer">ordered set serializer</dfn> takes a
<var title="">set</var> and returns the concatenation of the strings in
<var title="">set</var>, separated from each other by U+0020.
<h3 id="namespaces"><span class="secno">2.4 </span>Namespaces</h3>
<p>The <dfn id="html-namespace">HTML namespace</dfn> is
<code title="">http://www.w3.org/1999/xhtml</code>.
<p>The <dfn id="xml-namespace">XML namespace</dfn> is
<code title="">http://www.w3.org/XML/1998/namespace</code>.
<p>The <dfn id="xmlns-namespace">XMLNS namespace</dfn> is
<code title="">http://www.w3.org/2000/xmlns/</code>.
<h2 id="errors"><span class="secno">3 </span>Errors</h2>
<p class="XXX">This entire section will move out of DOM into IDL. <code><a href="#domexception">DOMException</a></code>
will be defined in terms of JavaScript and made available in both document and worker
environments. <code><a href="#domerror">DOMError</a></code> will go away.
<h3 id="exception-domexception"><span class="secno">3.1 </span>Exception <code><a href="#domexception">DOMException</a></code></h3>
<pre class="idl">exception <dfn id="domexception">DOMException</dfn> {
const unsigned short <a href="#dom-domexception-index_size_err" title="dom-DOMException-INDEX_SIZE_ERR">INDEX_SIZE_ERR</a> = 1;
const unsigned short <span title="dom-DOMException-DOMSTRING_SIZE_ERR">DOMSTRING_SIZE_ERR</span> = 2; // historical
const unsigned short <a href="#dom-domexception-hierarchy_request_err" title="dom-DOMException-HIERARCHY_REQUEST_ERR">HIERARCHY_REQUEST_ERR</a> = 3;
const unsigned short <a href="#dom-domexception-wrong_document_err" title="dom-DOMException-WRONG_DOCUMENT_ERR">WRONG_DOCUMENT_ERR</a> = 4;
const unsigned short <a href="#dom-domexception-invalid_character_err" title="dom-DOMException-INVALID_CHARACTER_ERR">INVALID_CHARACTER_ERR</a> = 5;
const unsigned short <span title="dom-DOMException-NO_DATA_ALLOWED_ERR">NO_DATA_ALLOWED_ERR</span> = 6; // historical
const unsigned short <a href="#dom-domexception-no_modification_allowed_err" title="dom-DOMException-NO_MODIFICATION_ALLOWED_ERR">NO_MODIFICATION_ALLOWED_ERR</a> = 7;
const unsigned short <a href="#dom-domexception-not_found_err" title="dom-DOMException-NOT_FOUND_ERR">NOT_FOUND_ERR</a> = 8;
const unsigned short <a href="#dom-domexception-not_supported_err" title="dom-DOMException-NOT_SUPPORTED_ERR">NOT_SUPPORTED_ERR</a> = 9;
const unsigned short <span title="dom-DOMException-INUSE_ATTRIBUTE_ERR">INUSE_ATTRIBUTE_ERR</span> = 10; // historical
const unsigned short <a href="#dom-domexception-invalid_state_err" title="dom-DOMException-INVALID_STATE_ERR">INVALID_STATE_ERR</a> = 11;
const unsigned short <a href="#dom-domexception-syntax_err" title="dom-DOMException-SYNTAX_ERR">SYNTAX_ERR</a> = 12;
const unsigned short <a href="#dom-domexception-invalid_modification_err" title="dom-DOMException-INVALID_MODIFICATION_ERR">INVALID_MODIFICATION_ERR</a> = 13;
const unsigned short <a href="#dom-domexception-namespace_err" title="dom-DOMException-NAMESPACE_ERR">NAMESPACE_ERR</a> = 14;
const unsigned short <a href="#dom-domexception-invalid_access_err" title="dom-DOMException-INVALID_ACCESS_ERR">INVALID_ACCESS_ERR</a> = 15;
const unsigned short <span title="dom-DOMException-VALIDATION_ERR">VALIDATION_ERR</span> = 16; // historical
const unsigned short <span title="dom-DOMException-TYPE_MISMATCH_ERR">TYPE_MISMATCH_ERR</span> = 17; // historical; use JavaScript's TypeError instead
const unsigned short <a href="#dom-domexception-security_err" title="dom-DOMException-SECURITY_ERR">SECURITY_ERR</a> = 18;
const unsigned short <a href="#dom-domexception-network_err" title="dom-DOMException-NETWORK_ERR">NETWORK_ERR</a> = 19;
const unsigned short <a href="#dom-domexception-abort_err" title="dom-DOMException-ABORT_ERR">ABORT_ERR</a> = 20;
const unsigned short <a href="#dom-domexception-url_mismatch_err" title="dom-DOMException-URL_MISMATCH_ERR">URL_MISMATCH_ERR</a> = 21;
const unsigned short <a href="#dom-domexception-quota_exceeded_err" title="dom-DOMException-QUOTA_EXCEEDED_ERR">QUOTA_EXCEEDED_ERR</a> = 22;
const unsigned short <a href="#dom-domexception-timeout_err" title="dom-DOMException-TIMEOUT_ERR">TIMEOUT_ERR</a> = 23;
const unsigned short <a href="#dom-domexception-invalid_node_type_err" title="dom-DOMException-INVALID_NODE_TYPE_ERR">INVALID_NODE_TYPE_ERR</a> = 24;
const unsigned short <a href="#dom-domexception-data_clone_err" title="dom-DOMException-DATA_CLONE_ERR">DATA_CLONE_ERR</a> = 25;
unsigned short <a href="#dom-domexception-code" title="dom-DOMException-code">code</a>;
};</pre>
<p>The <dfn id="dom-domexception-code" title="dom-DOMException-code"><code>code</code></dfn> exception
field must return the value it was initialized to.
<p>To <dfn id="concept-throw" title="concept-throw">throw</dfn> a <var title="">name</var> exception
run these steps:
<ol>
<li><p>Let <var title="">code</var> be zero.
<li><p>If <var title="">name</var> is in the first column of the
<a href="#error-names-table">error names table</a> and has a corresponding legacy
<code title="dom-DOMException-code"><a href="#dom-domexception-code">code</a></code> exception field value in the
third column, set <var title="">code</var> to that value.
<li><p><a class="external" data-anolis-spec="webidl" href="http://heycam.github.io/webidl/#dfn-throw">Throw</a> a new
<code><a href="#domexception">DOMException</a></code> exception, whose
<a class="external" data-anolis-spec="webidl" href="http://heycam.github.io/webidl/#dfn-exception-message">message</a> is a user agent-defined value,
<a class="external" data-anolis-spec="webidl" href="http://heycam.github.io/webidl/#dfn-exception-name">name</a> is <var title="">name</var>, and
whose <code title="dom-DOMException-code"><a href="#dom-domexception-code">code</a></code> exception field is
<var title="">code</var>.
</ol>
<p class="example">To
<i title="">throw a "<code><a href="#timeouterror">TimeoutError</a></code>" exception</i>, a
user agent would construct a <code><a href="#domexception">DOMException</a></code> exception whose
<a class="external" data-anolis-spec="webidl" href="http://heycam.github.io/webidl/#dfn-exception-name">name</a> is
"<code><a href="#timeouterror">TimeoutError</a></code>" and
<code title="dom-DOMException-code"><a href="#dom-domexception-code">code</a></code> exception field value is 23,
and actually throw that object as an exception. In JavaScript, this
exception will have a <code title="">name</code> property whose value is
"<code><a href="#timeouterror">TimeoutError</a></code>".
<h3 id="interface-domerror"><span class="secno">3.2 </span>Interface <code><a href="#domerror">DOMError</a></code></h3>
<pre class="idl">[<a href="#dom-domerror" title="dom-DOMError">Constructor</a>(DOMString <var title="">name</var>, optional DOMString <var title="">message</var> = "")]
interface <dfn id="domerror">DOMError</dfn> {
readonly attribute DOMString <a href="#dom-domerror-name" title="dom-DOMError-name">name</a>;
readonly attribute DOMString <a href="#dom-domerror-message" title="dom-DOMError-message">message</a>;
};</pre>
<p class="XXX"><code><a href="#domerror">DOMError</a></code> will be nuked in favor of using
<code><a href="#domexception">DOMException</a></code> exclusively. See <a href="https://mail.mozilla.org/pipermail/es-discuss/2013-July/thread.html#31677">Creating your own errors</a>
on es-discuss for more details.
<p class="note">This interface is intended for other specifications that want
to introduce error handling through other means than exceptions. As with
exceptions, the <a href="#error-names-table">error names table</a> is used.
<p>The <dfn id="dom-domerror" title="dom-DOMError"><code>DOMError(<var title="">name</var>, <var title="">message</var>)</code></dfn>
constructor must return a new <code><a href="#domerror">DOMError</a></code> object whose
<code title="dom-DOMError-name"><a href="#dom-domerror-name">name</a></code> attribute is initialized to
<var title="">name</var> and whose <code title="dom-DOMError-message"><a href="#dom-domerror-message">message</a></code> attribute
is initialized to <var title="">message</var>.
<p>The <dfn id="dom-domerror-name" title="dom-DOMError-name"><code>name</code></dfn> attribute must
return the value it was initialized to.
<p>The <dfn id="dom-domerror-message" title="dom-DOMError-message"><code>message</code></dfn> attribute must
return the value it was initialized to.
<p class="note">The value of the <code title="dom-DOMError-message"><a href="#dom-domerror-message">message</a></code> will
typically be implementation-dependent and for informational purposes only.
<p>A
<dfn id="concept-domerror" title="concept-DOMError"><var title="">name</var> <code>DOMError</code></dfn>
means a <code><a href="#domerror">DOMError</a></code> object whose
<code title="dom-DOMError-name"><a href="#dom-domerror-name">name</a></code> attribute is initialized to
<var title="">name</var> and whose <code title="dom-DOMError-message"><a href="#dom-domerror-message">message</a></code> attribute
is initialized to a helpful implementation-dependent message that explains the error.
<p class="example">A specification could say that an <code title="">error</code>
attribute must return a
<a href="#concept-domerror" title="concept-DOMError">"<code>SyntaxError</code>" <code>DOMError</code></a>.
<h3 id="error-names-0"><span class="secno">3.3 </span>Error names</h3>
<p>The <dfn id="error-names-table">error names table</dfn> below lists all the allowed error names,
a description, and legacy <code title="dom-DOMException-code"><a href="#dom-domexception-code">code</a></code>
exception field values (when the error name is used for
<a href="#concept-throw" title="concept-throw">throwing</a> an exception).
<p class="note">If an error name is not listed here, please file a bug as
indicated at the top of this specification and it will be addressed shortly.
Thanks!
<table id="error-names">
<thead>
<tr><th>Name<th>Description<th>Legacy <code title="dom-DOMException-code"><a href="#dom-domexception-code">code</a></code> exception field value (if any)
<tbody>
<tr>
<td>"<dfn id="indexsizeerror"><code>IndexSizeError</code></dfn>"
<td>The index is not in the allowed range.
<td><dfn id="dom-domexception-index_size_err" title="dom-DOMException-INDEX_SIZE_ERR"><code>INDEX_SIZE_ERR</code></dfn> (1)
<tr>
<td>"<dfn id="hierarchyrequesterror"><code>HierarchyRequestError</code></dfn>"
<td>The operation would yield an incorrect <a href="#concept-node-tree" title="concept-node-tree">node tree</a>.
<td><dfn id="dom-domexception-hierarchy_request_err" title="dom-DOMException-HIERARCHY_REQUEST_ERR"><code>HIERARCHY_REQUEST_ERR</code></dfn> (3)
<tr>
<td>"<dfn id="wrongdocumenterror"><code>WrongDocumentError</code></dfn>"
<td>The object is in the wrong <a href="#concept-document" title="concept-document">document</a>.
<td><dfn id="dom-domexception-wrong_document_err" title="dom-DOMException-WRONG_DOCUMENT_ERR"><code>WRONG_DOCUMENT_ERR</code></dfn> (4)
<tr>
<td>"<dfn id="invalidcharactererror"><code>InvalidCharacterError</code></dfn>"
<td>The string contains invalid characters.
<td><dfn id="dom-domexception-invalid_character_err" title="dom-DOMException-INVALID_CHARACTER_ERR"><code>INVALID_CHARACTER_ERR</code></dfn> (5)
<tr>
<td>"<dfn id="nomodificationallowederror"><code>NoModificationAllowedError</code></dfn>"
<td>The object can not be modified.
<td><dfn id="dom-domexception-no_modification_allowed_err" title="dom-DOMException-NO_MODIFICATION_ALLOWED_ERR"><code>NO_MODIFICATION_ALLOWED_ERR</code></dfn> (7)
<tr>
<td>"<dfn id="notfounderror"><code>NotFoundError</code></dfn>"
<td>The object can not be found here.
<td><dfn id="dom-domexception-not_found_err" title="dom-DOMException-NOT_FOUND_ERR"><code>NOT_FOUND_ERR</code></dfn> (8)
<tr>
<td>"<dfn id="notsupportederror"><code>NotSupportedError</code></dfn>"
<td>The operation is not supported.
<td><dfn id="dom-domexception-not_supported_err" title="dom-DOMException-NOT_SUPPORTED_ERR"><code>NOT_SUPPORTED_ERR</code></dfn> (9)
<tr>
<td>"<dfn id="invalidstateerror"><code>InvalidStateError</code></dfn>"
<td>The object is in an invalid state.
<td><dfn id="dom-domexception-invalid_state_err" title="dom-DOMException-INVALID_STATE_ERR"><code>INVALID_STATE_ERR</code></dfn> (11)
<tr>
<td>"<dfn id="syntaxerror"><code>SyntaxError</code></dfn>"
<td>The string did not match the expected pattern.
<td><dfn id="dom-domexception-syntax_err" title="dom-DOMException-SYNTAX_ERR"><code>SYNTAX_ERR</code></dfn> (12)
<tr>
<td>"<dfn id="invalidmodificationerror"><code>InvalidModificationError</code></dfn>"
<td>The object can not be modified in this way.
<td><dfn id="dom-domexception-invalid_modification_err" title="dom-DOMException-INVALID_MODIFICATION_ERR"><code>INVALID_MODIFICATION_ERR</code></dfn> (13)
<tr>
<td>"<dfn id="namespaceerror"><code>NamespaceError</code></dfn>"
<td>The operation is not allowed by <cite>Namespaces in XML</cite>. <a href="#refsXMLNS">[XMLNS]</a>
<td><dfn id="dom-domexception-namespace_err" title="dom-DOMException-NAMESPACE_ERR"><code>NAMESPACE_ERR</code></dfn> (14)
<tr>
<td>"<dfn id="invalidaccesserror"><code>InvalidAccessError</code></dfn>"
<td>The object does not support the operation or argument.
<td><dfn id="dom-domexception-invalid_access_err" title="dom-DOMException-INVALID_ACCESS_ERR"><code>INVALID_ACCESS_ERR</code></dfn> (15)
<tr>
<!-- XHR -->
<td>"<dfn id="securityerror"><code>SecurityError</code></dfn>"
<td>The operation is insecure.
<td><dfn id="dom-domexception-security_err" title="dom-DOMException-SECURITY_ERR"><code>SECURITY_ERR</code></dfn> (18)
<tr>
<!-- XHR -->
<td>"<dfn id="networkerror"><code>NetworkError</code></dfn>"
<td>A network error occurred.
<td><dfn id="dom-domexception-network_err" title="dom-DOMException-NETWORK_ERR"><code>NETWORK_ERR</code></dfn> (19)
<tr>
<!-- XHR -->
<td>"<dfn id="aborterror"><code>AbortError</code></dfn>"
<td>The operation was aborted.
<td><dfn id="dom-domexception-abort_err" title="dom-DOMException-ABORT_ERR"><code>ABORT_ERR</code></dfn> (20)
<tr>
<!-- Workers -->
<td>"<dfn id="urlmismatcherror"><code>URLMismatchError</code></dfn>"
<td>The given URL does not match another URL.
<td><dfn id="dom-domexception-url_mismatch_err" title="dom-DOMException-URL_MISMATCH_ERR"><code>URL_MISMATCH_ERR</code></dfn> (21)
<tr>
<!-- HTML -->
<td>"<dfn id="quotaexceedederror"><code>QuotaExceededError</code></dfn>"
<td>The quota has been exceeded.
<td><dfn id="dom-domexception-quota_exceeded_err" title="dom-DOMException-QUOTA_EXCEEDED_ERR"><code>QUOTA_EXCEEDED_ERR</code></dfn> (22)
<tr>
<!-- XHR -->
<td>"<dfn id="timeouterror"><code>TimeoutError</code></dfn>"
<td>The operation timed out.
<td><dfn id="dom-domexception-timeout_err" title="dom-DOMException-TIMEOUT_ERR"><code>TIMEOUT_ERR</code></dfn> (23)
<tr>
<td>"<dfn id="invalidnodetypeerror"><code>InvalidNodeTypeError</code></dfn>"
<td>The supplied node is incorrect or has an incorrect ancestor for this operation.
<td><dfn id="dom-domexception-invalid_node_type_err" title="dom-DOMException-INVALID_NODE_TYPE_ERR"><code>INVALID_NODE_TYPE_ERR</code></dfn> (24)
<tr>
<!-- HTML -->
<td>"<dfn id="datacloneerror"><code>DataCloneError</code></dfn>"
<td>The object can not be cloned.
<td><dfn id="dom-domexception-data_clone_err" title="dom-DOMException-DATA_CLONE_ERR"><code>DATA_CLONE_ERR</code></dfn> (25)
<tr>
<td>"<dfn id="encodingerror"><code>EncodingError</code></dfn>"
<td>The encoding operation (either encoded or decoding) failed.
<td>—
<tr>
<!-- FileAPI -->
<td>"<dfn id="notreadableerror"><code>NotReadableError</code></dfn>"
<td>The <code>File</code> or <code>Blob</code> could not be read.
<td>—
</table>
<h2 id="events"><span class="secno">4 </span>Events</h2>
<h3 id="introduction-to-dom-events"><span class="secno">4.1 </span>Introduction to "DOM Events"</h3>
<p>Throughout the web platform <a href="#concept-event" title="concept-event">events</a> are
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> to objects to signal an
occurrence, such as network activity or user interaction. These objects
implement the <code><a href="#eventtarget">EventTarget</a></code> interface and can therefore add
<a href="#concept-event-listener" title="concept-event-listener">event listeners</a> to observe
<a href="#concept-event" title="concept-event">events</a>:
<pre><code>obj.addEventListener("load", imgFetched)
function imgFetched(ev) {
// great success
…
}</code></pre>
<p><a href="#concept-event-listener" title="concept-event-listener">Event listeners</a> can be removed
by utilizing the
<code title="dom-EventTarget-removeEventListener"><a href="#dom-eventtarget-removeeventlistener">removeEventListener()</a></code>
method, passing the same arguments.
<p><a href="#concept-event" title="concept-event">Events</a> are objects too and implement the
<code><a href="#event">Event</a></code> interface (or a derived interface). In the example above
<var title="">ev</var> is the <a href="#concept-event" title="concept-event">event</a>. It is
passed as argument to
<a href="#concept-event-listener" title="concept-event-listener">event listener</a>'s <b>callback</b>
(typically a JavaScript Function as shown above).
<a href="#concept-event-listener" title="concept-event-listener">Event listeners</a> key off the
<a href="#concept-event" title="concept-event">event</a>'s
<code title="dom-Event-type"><a href="#dom-event-type">type</a></code> attribute value
("<code title="">load</code>" in the above example). The
<a href="#concept-event" title="concept-event">event</a>'s
<code title="dom-Event-target"><a href="#dom-event-target">target</a></code> attribute value returns the
object to which the <a href="#concept-event" title="concept-event">event</a> was
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> (<var title="">obj</var>
above).
<p>Now while typically <a href="#concept-event" title="concept-event">events</a> are
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> by the user agent as
the result of user interaction or the completion of some task, applications
can <a href="#concept-event-dispatch" title="concept-event-dispatch">dispatch</a>
<a href="#concept-event" title="concept-event">events</a> themselves, commonly known as
synthetic events:
<pre><code>// add an appropriate event listener
obj.addEventListener("cat", function(e) { process(e.detail) })
// create and dispatch the event
var event = new CustomEvent("cat", {"detail":{"hazcheeseburger":true}})
obj.dispatchEvent(event)</code></pre>
<p>Apart from signaling, <a href="#concept-event" title="concept-event">events</a> are
sometimes also used to let an application control what happens next in an
operation. For instance as part of form submission an
<a href="#concept-event" title="concept-event">event</a> whose
<code title="dom-Event-type"><a href="#dom-event-type">type</a></code> attribute value is
"<code title="">submit</code>" is
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a>. If this
<a href="#concept-event" title="concept-event">event</a>'s
<code title="dom-Event-preventDefault"><a href="#dom-event-preventdefault">preventDefault()</a></code> method is
invoked, form submission will be terminated. Applications who wish to make
use of this functionality through <a href="#concept-event" title="concept-event">events</a>
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> by the application
(synthetic events) can make use of the return value of the
<code title="dom-EventTarget-dispatchEvent"><a href="#dom-eventtarget-dispatchevent">dispatchEvent()</a></code> method:
<pre><code>if(obj.dispatchEvent(event)) {
// event was not canceled, time for some magic
…
}</code></pre>
<p>When an <a href="#concept-event" title="concept-event">event</a> is
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> to an object that
<a href="#concept-tree-participate" title="concept-tree-participate">participates</a> in a
<a href="#concept-tree" title="concept-tree">tree</a> (e.g. an
<a href="#concept-element" title="concept-element">element</a>), it can reach
<a href="#concept-event-listener" title="concept-event-listener">event listeners</a> on that object's
<a href="#concept-tree-ancestor" title="concept-tree-ancestor">ancestors</a> too. First all object's
<a href="#concept-tree-ancestor" title="concept-tree-ancestor">ancestor</a>
<a href="#concept-event-listener" title="concept-event-listener">event listeners</a> whose
<b>capture</b> variable is set to true are invoked, in
<a href="#concept-tree-order" title="concept-tree-order">tree order</a>. Second, object's own
<a href="#concept-event-listener" title="concept-event-listener">event listeners</a> are invoked. And
finally, and only if <a href="#concept-event" title="concept-event">event</a>'s
<a href="#dom-event-bubbles" title="dom-Event-bubbles">bubbles</a> attribute value is true,
object's <a href="#concept-tree-ancestor" title="concept-tree-ancestor">ancestor</a>
<a href="#concept-event-listener" title="concept-event-listener">event listeners</a> are invoked again,
but now in reverse <a href="#concept-tree-order" title="concept-tree-order">tree order</a>.
<p>Lets look at an example on how <a href="#concept-event" title="concept-event">events</a>
work in a <a href="#concept-tree" title="concept-tree">tree</a>:
<pre><code><!doctype html>
<html>
<head>
<title>Boring example</title>
</head>
<body>
<p>Hello <span id=x>world</span>!</p>
<script>
function test(e) {
debug(e.target, e.currentTarget, e.eventPhase)
}
document.addEventListener("hey", test, true)
document.body.addEventListener("hey", test)
var ev = new Event("hey", {bubbles:true})
document.getElementById("x").dispatchEvent(ev)
</script>
</body>
</html></code></pre>
<p>The <code title="">debug</code> function will be invoked twice. Each time
the <a href="#concept-event" title="concept-event">events</a>'s
<code title="dom-Event-target"><a href="#dom-event-target">target</a></code> attribute value will be the
<code title="">span</code> <a href="#concept-element" title="concept-element">element</a>. The
first time <code title="dom-Event-target"><a href="#dom-event-target">currentTarget</a></code> attribute's
value will be the <a href="#concept-document" title="concept-document">document</a>, the second
time the <code title="">body</code> <a href="#concept-element" title="concept-element">element</a>.
<code title="dom-Event-eventPhase"><a href="#dom-event-eventphase">eventPhase</a></code> attribute's value
switches from <code title="dom-Event-CAPTURING_PHASE"><a href="#dom-event-capturing_phase">CAPTURING_PHASE</a></code>
to <code title="dom-Event-BUBBLING_PHASE"><a href="#dom-event-bubbling_phase">BUBBLING_PHASE</a></code>. If an
<a href="#concept-event-listener" title="concept-event-listener">event listener</a> was registered for
the <code title="">span</code> <a href="#concept-element" title="concept-element">element</a>,
<code title="dom-Event-eventPhase"><a href="#dom-event-eventphase">eventPhase</a></code> attribute's value
would have been <code title="dom-Event-AT_TARGET"><a href="#dom-event-at_target">AT_TARGET</a></code>.
<h3 id="interface-event"><span class="secno">4.2 </span>Interface <code><a href="#event">Event</a></code></h3>
<pre class="idl">[<a href="#concept-event-constructor" title="concept-event-constructor">Constructor</a>(DOMString <var title="">type</var>, optional <a href="#eventinit">EventInit</a> <var title="">eventInitDict</var>),
Exposed=Window,Worker]
interface <dfn id="event">Event</dfn> {
readonly attribute DOMString <a href="#dom-event-type" title="dom-Event-type">type</a>;
readonly attribute <a href="#eventtarget">EventTarget</a>? <a href="#dom-event-target" title="dom-Event-target">target</a>;
readonly attribute <a href="#eventtarget">EventTarget</a>? <a href="#dom-event-currenttarget" title="dom-Event-currentTarget">currentTarget</a>;
const unsigned short <a href="#dom-event-none" title="dom-Event-NONE">NONE</a> = 0;
const unsigned short <a href="#dom-event-capturing_phase" title="dom-Event-CAPTURING_PHASE">CAPTURING_PHASE</a> = 1;
const unsigned short <a href="#dom-event-at_target" title="dom-Event-AT_TARGET">AT_TARGET</a> = 2;
const unsigned short <a href="#dom-event-bubbling_phase" title="dom-Event-BUBBLING_PHASE">BUBBLING_PHASE</a> = 3;
readonly attribute unsigned short <a href="#dom-event-eventphase" title="dom-Event-eventPhase">eventPhase</a>;
void <a href="#dom-event-stoppropagation" title="dom-Event-stopPropagation">stopPropagation</a>();
void <a href="#dom-event-stopimmediatepropagation" title="dom-Event-stopImmediatePropagation">stopImmediatePropagation</a>();
readonly attribute boolean <a href="#dom-event-bubbles" title="dom-Event-bubbles">bubbles</a>;
readonly attribute boolean <a href="#dom-event-cancelable" title="dom-Event-cancelable">cancelable</a>;
void <a href="#dom-event-preventdefault" title="dom-Event-preventDefault">preventDefault</a>();
readonly attribute boolean <a href="#dom-event-defaultprevented" title="dom-Event-defaultPrevented">defaultPrevented</a>;
[Unforgeable] readonly attribute boolean <a href="#dom-event-istrusted" title="dom-Event-isTrusted">isTrusted</a>;
readonly attribute DOMTimeStamp <a href="#dom-event-timestamp" title="dom-Event-timeStamp">timeStamp</a>;
void <a href="#dom-event-initevent" title="dom-Event-initEvent">initEvent</a>(DOMString <var title="">type</var>, boolean <var title="">bubbles</var>, boolean <var title="">cancelable</var>);
};
dictionary <dfn id="eventinit">EventInit</dfn> {
boolean <span title="dom-EventInit-bubbles">bubbles</span> = false;
boolean <span title="dom-EventInit-cancelable">cancelable</span> = false;
};</pre>
<p>An <dfn id="concept-event" title="concept-event">event</dfn> allows for signaling that
something has occurred. E.g. that an image has completed downloading. It is
represented by the <code><a href="#event">Event</a></code> interface or an interface that
inherits from the <code><a href="#event">Event</a></code> interface.</p>
<dl class="domintro">
<dt><code>var <var title="">event</var> = new <a href="#concept-event-constructor" title="concept-event-constructor">Event</a>(<var title="">type</var> [, <var title="">eventInitDict</var>])</code>
<dd><p>Returns a new <var title="">event</var> whose
<code title="dom-Event-type"><a href="#dom-event-type">type</a></code> attribute value is set to
<var title="">type</var>. The optional <var title="">eventInitDict</var> argument
allows for setting the <code title="dom-Event-bubbles"><a href="#dom-event-bubbles">bubbles</a></code> and
<code title="dom-Event-cancelable"><a href="#dom-event-cancelable">cancelable</a></code> attributes via object
members of the same name.
<dt><code><var title="">event</var> . <a href="#dom-event-type" title="dom-Event-type">type</a></code>
<dd><p>Returns the type of <var title="">event</var>, e.g.
"<code title="">click</code>", "<code title="">hashchange</code>", or
"<code title="">submit</code>".
<dt><code><var title="">event</var> . <a href="#dom-event-target" title="dom-Event-target">target</a></code>
<dd><p>Returns the object <var title="">event</var> is
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> to.
<dt><code><var title="">event</var> . <a href="#dom-event-currenttarget" title="dom-Event-currentTarget">currentTarget</a></code>
<dd><p>Returns the object whose
<a href="#concept-event-listener" title="concept-event-listener">event listener</a>'s <b>callback</b>
is invoked.
<dt><code><var title="">event</var> . <a href="#dom-event-eventphase" title="dom-Event-eventPhase">eventPhase</a></code>
<dd><p>Returns the <a href="#concept-event" title="concept-event">event</a>'s phase, which is
one of <code title="dom-Event-NONE"><a href="#dom-event-none">NONE</a></code>,
<code title="dom-Event-CAPTURING_PHASE"><a href="#dom-event-capturing_phase">CAPTURING_PHASE</a></code>,
<code title="dom-Event-AT_TARGET"><a href="#dom-event-at_target">AT_TARGET</a></code>, and
<code title="dom-Event-BUBBLING_PHASE"><a href="#dom-event-bubbling_phase">BUBBLING_PHASE</a></code>.
<dt><code><var title="">event</var> . <a href="#dom-event-stoppropagation" title="dom-Event-stopPropagation">stopPropagation</a>()</code>
<dd><p>When <a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> in a
<a href="#concept-tree" title="concept-tree">tree</a>, invoking this method prevents
<var title="">event</var> from reaching any other objects than the current.
<dt><code><var title="">event</var> . <a href="#dom-event-stopimmediatepropagation" title="dom-Event-stopImmediatePropagation">stopImmediatePropagation</a>()</code>
<dd><p>Invoking this method prevents <var title="">event</var> from reaching
any <a href="#concept-event-listener" title="concept-event-listener">event listeners</a> registered
after the current one and when
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> in a
<a href="#concept-tree" title="concept-tree">tree</a> also prevents
<var title="">event</var> from reaching any other objects.
<dt><code><var title="">event</var> . <a href="#dom-event-bubbles" title="dom-Event-bubbles">bubbles</a></code>
<dd><p>Returns true if <var title="">event</var>'s goes through its <code title="dom-Event-target"><a href="#dom-event-target">target</a></code> attribute value's <a href="#concept-tree-ancestor" title="concept-tree-ancestor">ancestors</a> in reverse <a href="#concept-tree-order" title="concept-tree-order">tree order</a>, and false otherwise.
<dt><code><var title="">event</var> . <a href="#dom-event-cancelable" title="dom-Event-cancelable">cancelable</a></code>
<dd><p>Returns true or false depending on how <var title="">event</var> was
initialized. Its return value does not always carry meaning, but true can
indicate that part of the operation during which <var title="">event</var> was
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a>, can be canceled by
invoking the <code title="dom-Event-preventDefault"><a href="#dom-event-preventdefault">preventDefault()</a></code>
method.
<dt><code><var title="">event</var> . <a href="#dom-event-preventdefault" title="dom-Event-preventDefault">preventDefault</a>()</code>
<dd><p>If invoked when the
<code title="dom-Event-cancelable"><a href="#dom-event-cancelable">cancelable</a></code> attribute value is true,
signals to the operation that caused <var title="">event</var> to be
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> that it needs to be
canceled.
<dt><code><var title="">event</var> . <a href="#dom-event-defaultprevented" title="dom-Event-defaultPrevented">defaultPrevented</a></code>
<dd><p>Returns true if
<code title="dom-Event-preventDefault"><a href="#dom-event-preventdefault">preventDefault()</a></code> was invoked
while the <code title="dom-Event-cancelable"><a href="#dom-event-cancelable">cancelable</a></code> attribute
value is true, and false otherwise.
<dt><code><var title="">event</var> . <a href="#dom-event-istrusted" title="dom-Event-isTrusted">isTrusted</a></code>
<dd><p>Returns true if <var title="">event</var> was
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> by the user agent, and
false otherwise.
<dt><code><var title="">event</var> . <a href="#dom-event-timestamp" title="dom-Event-timeStamp">timeStamp</a></code>
<dd><p>Returns the creation time of <var title="">event</var> in the number of
milliseconds that passed since 00:00:00 UTC on 1 January 1970.
<!-- initEvent is dead -->
</dl>
<p>The <dfn id="dom-event-type" title="dom-Event-type"><code>type</code></dfn> attribute must
return the value it was initialized to. When an
<a href="#concept-event" title="concept-event">event</a> is created the attribute must be
initialized to the empty string.
<p>The <dfn id="dom-event-target" title="dom-Event-target"><code>target</code></dfn> and
<dfn id="dom-event-currenttarget" title="dom-Event-currentTarget"><code>currentTarget</code></dfn>
attributes must return the values they were initialized to. When an
<a href="#concept-event" title="concept-event">event</a> is created the attributes must be
initialized to null.
<p>The <dfn id="dom-event-eventphase" title="dom-Event-eventPhase"><code>eventPhase</code></dfn>
attribute must return the value it was initialized to, which must be one of
the following:</p>
<dl>
<dt><dfn id="dom-event-none" title="dom-Event-NONE"><code>NONE</code></dfn> (numeric value 0)
<dd><p><a href="#concept-event" title="concept-event">Events</a> not currently
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> are in this phase.
<dt><dfn id="dom-event-capturing_phase" title="dom-Event-CAPTURING_PHASE"><code>CAPTURING_PHASE</code></dfn> (numeric value 1)</dt>
<dd><p>When an <a href="#concept-event" title="concept-event">event</a> is
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> to an object that
<a href="#concept-tree-participate" title="concept-tree-participate">participates</a> in a
<a href="#concept-tree" title="concept-tree">tree</a> it will be in this phase before it
reaches its <code title="dom-Event-target"><a href="#dom-event-target">target</a></code> attribute value.
<dt><dfn id="dom-event-at_target" title="dom-Event-AT_TARGET"><code>AT_TARGET</code></dfn> (numeric value 2)
<dd><p>When an <a href="#concept-event" title="concept-event">event</a> is
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> it will be in this
phase on its <code title="dom-Event-target"><a href="#dom-event-target">target</a></code> attribute value.
<dt><dfn id="dom-event-bubbling_phase" title="dom-Event-BUBBLING_PHASE"><code>BUBBLING_PHASE</code></dfn> (numeric value 3)
<dd><p>When an <a href="#concept-event" title="concept-event">event</a> is
<a href="#concept-event-dispatch" title="concept-event-dispatch">dispatched</a> to an object that
<a href="#concept-tree-participate" title="concept-tree-participate">participates</a> in a
<a href="#concept-tree" title="concept-tree">tree</a> it will be in this phase after it
reaches its <code title="dom-Event-target"><a href="#dom-event-target">target</a></code> attribute value.
</dl>
<p>Initially the attribute must be initialized to
<code title="dom-Event-NONE"><a href="#dom-event-none">NONE</a></code>.
<hr>
<p>Each <a href="#concept-event" title="concept-event">event</a> has the following associated
flags that are all initially unset:</p>
<ul>
<li><dfn id="stop-propagation-flag">stop propagation flag</dfn>
<li><dfn id="stop-immediate-propagation-flag">stop immediate propagation flag</dfn>
<li><dfn id="canceled-flag">canceled flag</dfn>
<li><dfn id="initialized-flag">initialized flag</dfn>
<li><dfn id="dispatch-flag">dispatch flag</dfn>
</ul>
<p>The
<dfn id="dom-event-stoppropagation" title="dom-Event-stopPropagation"><code>stopPropagation()</code></dfn>
method must set the <a href="#stop-propagation-flag">stop propagation flag</a>.
<p>The
<dfn id="dom-event-stopimmediatepropagation" title="dom-Event-stopImmediatePropagation"><code>stopImmediatePropagation()</code></dfn>
method must set both the <a href="#stop-propagation-flag">stop propagation flag</a> and
<a href="#stop-immediate-propagation-flag">stop immediate propagation flag</a>.
<p>The <dfn id="dom-event-bubbles" title="dom-Event-bubbles"><code>bubbles</code></dfn> and
<dfn id="dom-event-cancelable" title="dom-Event-cancelable"><code>cancelable</code></dfn> attributes
must return the values they were initialized to. When an
<a href="#concept-event" title="concept-event">event</a> is created, they must be initialized
to false. <span class="note">The <code><a href="#eventinit">EventInit</a></code> dictionary is not sufficient due to
the existence of the legacy
<code title="dom-Document-createEvent"><a href="#dom-document-createevent">createEvent()</a></code>.</span>
<p>The
<dfn id="dom-event-preventdefault" title="dom-Event-preventDefault"><code>preventDefault()</code></dfn>
method must set the <a href="#canceled-flag">canceled flag</a> if the
<code title="dom-Event-cancelable"><a href="#dom-event-cancelable">cancelable</a></code> attribute value is true.
<p>The
<dfn id="dom-event-defaultprevented" title="dom-Event-defaultPrevented"><code>defaultPrevented</code></dfn>
attribute must return true if the <a href="#canceled-flag">canceled flag</a> is set and
false otherwise.
<hr>
<p>The <dfn id="dom-event-istrusted" title="dom-Event-isTrusted"><code>isTrusted</code></dfn> attribute
must return the value it was initialized to. When an