Annotation of html5/spec/scripting-1.html, revision 1.40
1.1 mike 1: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
2: <!DOCTYPE html>
1.36 mike 3: <!-- when publishing, change bits marked ZZZ
4: ZZZ: Set PUB to 1 for TR/ drafts, 0 for dev drafts; PUB-Y lines are used if it's 1 and PUB-N lines if it's 0.
5: ZZZ: Set YEAR, SHORTDAY (month/day), and LONGDAY accordingly. They are used by the INSERT FOO bits below.
6: --><html lang="en-US-x-Hixie" class="split chapter"><head><title>4.3 Scripting — HTML5 </title><style type="text/css">
1.1 mike 7: pre { margin-left: 2em; white-space: pre-wrap; }
8: h2 { margin: 3em 0 1em 0; }
9: h3 { margin: 2.5em 0 1em 0; }
10: h4 { margin: 2.5em 0 0.75em 0; }
11: h5, h6 { margin: 2.5em 0 1em; }
12: h1 + h2, h1 + h2 + h2 { margin: 0.75em 0 0.75em; }
13: h2 + h3, h3 + h4, h4 + h5, h5 + h6 { margin-top: 0.5em; }
14: p { margin: 1em 0; }
15: hr:not(.top) { display: block; background: none; border: none; padding: 0; margin: 2em 0; height: auto; }
16: dl, dd { margin-top: 0; margin-bottom: 0; }
17: dt { margin-top: 0.75em; margin-bottom: 0.25em; clear: left; }
18: dt + dt { margin-top: 0; }
19: dd dt { margin-top: 0.25em; margin-bottom: 0; }
20: dd p { margin-top: 0; }
21: dd dl + p { margin-top: 1em; }
22: dd table + p { margin-top: 1em; }
23: p + * > li, dd li { margin: 1em 0; }
24: dt, dfn { font-weight: bold; font-style: normal; }
25: dt dfn { font-style: italic; }
26: pre, code { font-size: inherit; font-family: monospace; font-variant: normal; }
27: pre strong { color: black; font: inherit; font-weight: bold; background: yellow; }
28: pre em { font-weight: bolder; font-style: normal; }
29: @media screen { code { color: orangered; } code :link, code :visited { color: inherit; } }
30: var sub { vertical-align: bottom; font-size: smaller; position: relative; top: 0.1em; }
31: table { border-collapse: collapse; border-style: hidden hidden none hidden; }
32: table thead, table tbody { border-bottom: solid; }
33: table tbody th:first-child { border-left: solid; }
34: table tbody th { text-align: left; }
35: table td, table th { border-left: solid; border-right: solid; border-bottom: solid thin; vertical-align: top; padding: 0.2em; }
36: blockquote { margin: 0 0 0 2em; border: 0; padding: 0; font-style: italic; }
37:
38: .bad, .bad *:not(.XXX) { color: gray; border-color: gray; background: transparent; }
39: .matrix, .matrix td { border: none; text-align: right; }
40: .matrix { margin-left: 2em; }
41: .dice-example { border-collapse: collapse; border-style: hidden solid solid hidden; border-width: thin; margin-left: 3em; }
42: .dice-example caption { width: 30em; font-size: smaller; font-style: italic; padding: 0.75em 0; text-align: left; }
43: .dice-example td, .dice-example th { border: solid thin; width: 1.35em; height: 1.05em; text-align: center; padding: 0; }
44:
45: .toc dfn, h1 dfn, h2 dfn, h3 dfn, h4 dfn, h5 dfn, h6 dfn { font: inherit; }
46: img.extra { float: right; }
47: pre.idl { border: solid thin; background: #EEEEEE; color: black; padding: 0.5em 1em; }
48: pre.idl :link, pre.idl :visited { color: inherit; background: transparent; }
49: pre.css { border: solid thin; background: #FFFFEE; color: black; padding: 0.5em 1em; }
50: pre.css:first-line { color: #AAAA50; }
51: dl.domintro { color: green; margin: 2em 0 2em 2em; padding: 0.5em 1em; border: none; background: #DDFFDD; }
52: hr + dl.domintro, div.impl + dl.domintro { margin-top: 2.5em; margin-bottom: 1.5em; }
53: dl.domintro dt, dl.domintro dt * { color: black; text-decoration: none; }
54: dl.domintro dd { margin: 0.5em 0 1em 2em; padding: 0; }
55: dl.domintro dd p { margin: 0.5em 0; }
56: dl.switch { padding-left: 2em; }
57: dl.switch > dt { text-indent: -1.5em; }
58: dl.switch > dt:before { content: '\21AA'; padding: 0 0.5em 0 0; display: inline-block; width: 1em; text-align: right; line-height: 0.5em; }
59: dl.triple { padding: 0 0 0 1em; }
60: dl.triple dt, dl.triple dd { margin: 0; display: inline }
61: dl.triple dt:after { content: ':'; }
62: dl.triple dd:after { content: '\A'; white-space: pre; }
63: .diff-old { text-decoration: line-through; color: silver; background: transparent; }
64: .diff-chg, .diff-new { text-decoration: underline; color: green; background: transparent; }
65: a .diff-new { border-bottom: 1px blue solid; }
66:
67: h2 { page-break-before: always; }
68: h1, h2, h3, h4, h5, h6 { page-break-after: avoid; }
69: h1 + h2, hr + h2.no-toc { page-break-before: auto; }
70:
1.29 mike 71: p > span:not([title=""]):not([class="XXX"]):not([class="impl"]):not([class="note"]),
72: li > span:not([title=""]):not([class="XXX"]):not([class="impl"]):not([class="note"]), { border-bottom: solid #9999CC; }
1.1 mike 73:
74: div.head { margin: 0 0 1em; padding: 1em 0 0 0; }
75: div.head p { margin: 0; }
76: div.head h1 { margin: 0; }
77: div.head .logo { float: right; margin: 0 1em; }
78: div.head .logo img { border: none } /* remove border from top image */
79: div.head dl { margin: 1em 0; }
80: div.head p.copyright, div.head p.alt { font-size: x-small; font-style: oblique; margin: 0; }
81:
82: body > .toc > li { margin-top: 1em; margin-bottom: 1em; }
83: body > .toc.brief > li { margin-top: 0.35em; margin-bottom: 0.35em; }
84: body > .toc > li > * { margin-bottom: 0.5em; }
85: body > .toc > li > * > li > * { margin-bottom: 0.25em; }
86: .toc, .toc li { list-style: none; }
87:
88: .brief { margin-top: 1em; margin-bottom: 1em; line-height: 1.1; }
89: .brief li { margin: 0; padding: 0; }
90: .brief li p { margin: 0; padding: 0; }
91:
92: .category-list { margin-top: -0.75em; margin-bottom: 1em; line-height: 1.5; }
93: .category-list::before { content: '\21D2\A0'; font-size: 1.2em; font-weight: 900; }
94: .category-list li { display: inline; }
95: .category-list li:not(:last-child)::after { content: ', '; }
96: .category-list li > span, .category-list li > a { text-transform: lowercase; }
97: .category-list li * { text-transform: none; } /* don't affect <code> nested in <a> */
98:
99: .XXX { color: #E50000; background: white; border: solid red; padding: 0.5em; margin: 1em 0; }
100: .XXX > :first-child { margin-top: 0; }
101: p .XXX { line-height: 3em; }
102: .annotation { border: solid thin black; background: #0C479D; color: white; position: relative; margin: 8px 0 20px 0; }
103: .annotation:before { position: absolute; left: 0; top: 0; width: 100%; height: 100%; margin: 6px -6px -6px 6px; background: #333333; z-index: -1; content: ''; }
104: .annotation :link, .annotation :visited { color: inherit; }
105: .annotation :link:hover, .annotation :visited:hover { background: transparent; }
106: .annotation span { border: none ! important; }
107: .note { color: green; background: transparent; font-family: sans-serif; }
108: .warning { color: red; background: transparent; }
109: .note, .warning { font-weight: bolder; font-style: italic; }
110: p.note, div.note { padding: 0.5em 2em; }
111: span.note { padding: 0 2em; }
112: .note p:first-child, .warning p:first-child { margin-top: 0; }
113: .note p:last-child, .warning p:last-child { margin-bottom: 0; }
114: .warning:before { font-style: normal; }
115: p.note:before { content: 'Note: '; }
116: p.warning:before { content: '\26A0 Warning! '; }
117:
118: .bookkeeping:before { display: block; content: 'Bookkeeping details'; font-weight: bolder; font-style: italic; }
119: .bookkeeping { font-size: 0.8em; margin: 2em 0; }
120: .bookkeeping p { margin: 0.5em 2em; display: list-item; list-style: square; }
1.9 mike 121: .bookkeeping dt { margin: 0.5em 2em 0; }
122: .bookkeeping dd { margin: 0 3em 0.5em; }
1.1 mike 123:
124: h4 { position: relative; z-index: 3; }
125: h4 + .element, h4 + div + .element { margin-top: -2.5em; padding-top: 2em; }
126: .element {
127: background: #EEEEFF;
128: color: black;
129: margin: 0 0 1em 0.15em;
130: padding: 0 1em 0.25em 0.75em;
131: border-left: solid #9999FF 0.25em;
132: position: relative;
133: z-index: 1;
134: }
135: .element:before {
136: position: absolute;
137: z-index: 2;
138: top: 0;
139: left: -1.15em;
140: height: 2em;
141: width: 0.9em;
142: background: #EEEEFF;
143: content: ' ';
144: border-style: none none solid solid;
145: border-color: #9999FF;
146: border-width: 0.25em;
147: }
148:
149: .example { display: block; color: #222222; background: #FCFCFC; border-left: double; margin-left: 2em; padding-left: 1em; }
150: td > .example:only-child { margin: 0 0 0 0.1em; }
151:
152: ul.domTree, ul.domTree ul { padding: 0 0 0 1em; margin: 0; }
153: ul.domTree li { padding: 0; margin: 0; list-style: none; position: relative; }
154: ul.domTree li li { list-style: none; }
155: ul.domTree li:first-child::before { position: absolute; top: 0; height: 0.6em; left: -0.75em; width: 0.5em; border-style: none none solid solid; content: ''; border-width: 0.1em; }
156: ul.domTree li:not(:last-child)::after { position: absolute; top: 0; bottom: -0.6em; left: -0.75em; width: 0.5em; border-style: none none solid solid; content: ''; border-width: 0.1em; }
157: ul.domTree span { font-style: italic; font-family: serif; }
158: ul.domTree .t1 code { color: purple; font-weight: bold; }
159: ul.domTree .t2 { font-style: normal; font-family: monospace; }
160: ul.domTree .t2 .name { color: black; font-weight: bold; }
161: ul.domTree .t2 .value { color: blue; font-weight: normal; }
162: ul.domTree .t3 code, .domTree .t4 code, .domTree .t5 code { color: gray; }
163: ul.domTree .t7 code, .domTree .t8 code { color: green; }
164: ul.domTree .t10 code { color: teal; }
165:
166: body.dfnEnabled dfn { cursor: pointer; }
167: .dfnPanel {
168: display: inline;
169: position: absolute;
170: z-index: 10;
171: height: auto;
172: width: auto;
173: padding: 0.5em 0.75em;
174: font: small sans-serif, Droid Sans Fallback;
175: background: #DDDDDD;
176: color: black;
177: border: outset 0.2em;
178: }
179: .dfnPanel * { margin: 0; padding: 0; font: inherit; text-indent: 0; }
180: .dfnPanel :link, .dfnPanel :visited { color: black; }
181: .dfnPanel p { font-weight: bolder; }
182: .dfnPanel * + p { margin-top: 0.25em; }
183: .dfnPanel li { list-style-position: inside; }
184:
185: #configUI { position: absolute; z-index: 20; top: 10em; right: 1em; width: 11em; font-size: small; }
186: #configUI p { margin: 0.5em 0; padding: 0.3em; background: #EEEEEE; color: black; border: inset thin; }
187: #configUI p label { display: block; }
188: #configUI #updateUI, #configUI .loginUI { text-align: center; }
189: #configUI input[type=button] { display: block; margin: auto; }
1.8 mike 190:
1.33 mike 191: fieldset { margin: 1em; padding: 0.5em 1em; }
192: fieldset > legend + * { margin-top: 0; }
1.28 mike 193: fieldset > :last-child { margin-bottom: 0; }
1.33 mike 194: fieldset p { margin: 0.5em 0; }
195:
1.38 mike 196: </style><link href="http://www.w3.org/StyleSheets/TR/W3C-ED" rel="stylesheet" type="text/css"><style type="text/css">
1.1 mike 197:
198: .applies thead th > * { display: block; }
199: .applies thead code { display: block; }
200: .applies tbody th { whitespace: nowrap; }
201: .applies td { text-align: center; }
202: .applies .yes { background: yellow; }
203:
1.10 mike 204: .matrix, .matrix td { border: hidden; text-align: right; }
1.1 mike 205: .matrix { margin-left: 2em; }
206:
207: .dice-example { border-collapse: collapse; border-style: hidden solid solid hidden; border-width: thin; margin-left: 3em; }
208: .dice-example caption { width: 30em; font-size: smaller; font-style: italic; padding: 0.75em 0; text-align: left; }
209: .dice-example td, .dice-example th { border: solid thin; width: 1.35em; height: 1.05em; text-align: center; padding: 0; }
210:
1.17 mike 211: td.eg { border-width: thin; text-align: center; }
212:
1.1 mike 213: #table-example-1 { border: solid thin; border-collapse: collapse; margin-left: 3em; }
214: #table-example-1 * { font-family: "Essays1743", serif; line-height: 1.01em; }
215: #table-example-1 caption { padding-bottom: 0.5em; }
216: #table-example-1 thead, #table-example-1 tbody { border: none; }
217: #table-example-1 th, #table-example-1 td { border: solid thin; }
218: #table-example-1 th { font-weight: normal; }
219: #table-example-1 td { border-style: none solid; vertical-align: top; }
220: #table-example-1 th { padding: 0.5em; vertical-align: middle; text-align: center; }
221: #table-example-1 tbody tr:first-child td { padding-top: 0.5em; }
222: #table-example-1 tbody tr:last-child td { padding-bottom: 1.5em; }
223: #table-example-1 tbody td:first-child { padding-left: 2.5em; padding-right: 0; width: 9em; }
224: #table-example-1 tbody td:first-child::after { content: leader(". "); }
225: #table-example-1 tbody td { padding-left: 2em; padding-right: 2em; }
226: #table-example-1 tbody td:first-child + td { width: 10em; }
227: #table-example-1 tbody td:first-child + td ~ td { width: 2.5em; }
228: #table-example-1 tbody td:first-child + td + td + td ~ td { width: 1.25em; }
229:
230: .apple-table-examples { border: none; border-collapse: separate; border-spacing: 1.5em 0em; width: 40em; margin-left: 3em; }
231: .apple-table-examples * { font-family: "Times", serif; }
232: .apple-table-examples td, .apple-table-examples th { border: none; white-space: nowrap; padding-top: 0; padding-bottom: 0; }
233: .apple-table-examples tbody th:first-child { border-left: none; width: 100%; }
234: .apple-table-examples thead th:first-child ~ th { font-size: smaller; font-weight: bolder; border-bottom: solid 2px; text-align: center; }
235: .apple-table-examples tbody th::after, .apple-table-examples tfoot th::after { content: leader(". ") }
236: .apple-table-examples tbody th, .apple-table-examples tfoot th { font: inherit; text-align: left; }
237: .apple-table-examples td { text-align: right; vertical-align: top; }
238: .apple-table-examples.e1 tbody tr:last-child td { border-bottom: solid 1px; }
239: .apple-table-examples.e1 tbody + tbody tr:last-child td { border-bottom: double 3px; }
240: .apple-table-examples.e2 th[scope=row] { padding-left: 1em; }
241: .apple-table-examples sup { line-height: 0; }
242:
243: .details-example img { vertical-align: top; }
244:
1.39 mike 245: #base64-table {
246: white-space: nowrap;
247: font-size: 0.6em;
248: column-width: 6em;
249: column-count: 5;
250: column-gap: 1em;
251: -moz-column-width: 6em;
252: -moz-column-count: 5;
253: -moz-column-gap: 1em;
254: -webkit-column-width: 6em;
255: -webkit-column-count: 5;
256: -webkit-column-gap: 1em;
257: }
258: #base64-table thead { display: none; }
259: #base64-table * { border: none; }
260: #base64-table tbody td:first-child:after { content: ':'; }
261: #base64-table tbody td:last-child { text-align: right; }
262:
1.1 mike 263: #named-character-references-table {
1.24 mike 264: white-space: nowrap;
1.1 mike 265: font-size: 0.6em;
1.24 mike 266: column-width: 30em;
1.1 mike 267: column-gap: 1em;
1.24 mike 268: -moz-column-width: 30em;
1.1 mike 269: -moz-column-gap: 1em;
1.24 mike 270: -webkit-column-width: 30em;
1.1 mike 271: -webkit-column-gap: 1em;
272: }
1.24 mike 273: #named-character-references-table > table > tbody > tr > td:first-child + td,
1.1 mike 274: #named-character-references-table > table > tbody > tr > td:last-child { text-align: center; }
275: #named-character-references-table > table > tbody > tr > td:last-child:hover > span { position: absolute; top: auto; left: auto; margin-left: 0.5em; line-height: 1.2; font-size: 5em; border: outset; padding: 0.25em 0.5em; background: white; width: 1.25em; height: auto; text-align: center; }
1.24 mike 276: #named-character-references-table > table > tbody > tr#entity-CounterClockwiseContourIntegral > td:first-child { font-size: 0.5em; }
1.1 mike 277:
1.2 mike 278: .glyph.control { color: red; }
279:
1.4 mike 280: @font-face {
281: font-family: 'Essays1743';
282: src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743.ttf');
283: }
284: @font-face {
285: font-family: 'Essays1743';
286: font-weight: bold;
287: src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743-Bold.ttf');
288: }
289: @font-face {
290: font-family: 'Essays1743';
291: font-style: italic;
292: src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743-Italic.ttf');
293: }
294: @font-face {
295: font-family: 'Essays1743';
296: font-style: italic;
297: font-weight: bold;
298: src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743-BoldItalic.ttf');
299: }
300:
1.1 mike 301: </style><style type="text/css">
302: .domintro:before { display: table; margin: -1em -0.5em -0.5em auto; width: auto; content: 'This box is non-normative. Implementation requirements are given below this box.'; color: black; font-style: italic; border: solid 2px; background: white; padding: 0 0.25em; }
303: </style><link href="data:text/css," id="complete" rel="stylesheet" title="Complete specification"><link href="data:text/css,.impl%20%7B%20display:%20none;%20%7D%0Ahtml%20%7B%20border:%20solid%20yellow;%20%7D%20.domintro:before%20%7B%20display:%20none;%20%7D" id="author" rel="alternate stylesheet" title="Author documentation only"><link href="data:text/css,.impl%20%7B%20background:%20%23FFEEEE;%20%7D%20.domintro:before%20%7B%20background:%20%23FFEEEE;%20%7D" id="highlight" rel="alternate stylesheet" title="Highlight implementation requirements"><script type="text/javascript">
304: function getCookie(name) {
305: var params = location.search.substr(1).split("&");
306: for (var index = 0; index < params.length; index++) {
307: if (params[index] == name)
308: return "1";
309: var data = params[index].split("=");
310: if (data[0] == name)
311: return unescape(data[1]);
312: }
313: var cookies = document.cookie.split("; ");
314: for (var index = 0; index < cookies.length; index++) {
315: var data = cookies[index].split("=");
316: if (data[0] == name)
317: return unescape(data[1]);
318: }
319: return null;
320: }
321: function load(script) {
322: var e = document.createElement('script');
1.38 mike 323: e.setAttribute('src', 'http://www.whatwg.org/specs/web-apps/current-work/' + script + '?' + encodeURIComponent(location) + '&' + encodeURIComponent(document.referrer));
1.1 mike 324: document.body.appendChild(e);
325: }
326: function init() {
327: if (location.search == '?slow-browser')
328: return;
329: var configUI = document.createElement('div');
330: configUI.id = 'configUI';
331: document.body.appendChild(configUI);
332: // load('reviewer.js'); // would need cross-site XHR
333: if (document.getElementById('head'))
334: load('toc.js');
335: load('styler.js');
336: // load('updater.js'); // would need cross-site XHR
337: load('dfn.js'); // doesn't support split-out specs, but, oh well.
338: // load('status.js'); // would need cross-site XHR
339: if (getCookie('profile') == '1')
340: document.getElementsByTagName('h2')[0].textContent += '; load: ' + (new Date() - loadTimer) + 'ms';
341: fixBrokenLink();
342: }
1.36 mike 343: </script>
1.1 mike 344: <script src="link-fixup.js"></script>
1.35 mike 345: <link href="style.css" rel="stylesheet"><link href="semantics.html" title="4 The elements of HTML" rel="prev">
1.1 mike 346: <link href="spec.html#contents" title="Table of contents" rel="index">
347: <link href="sections.html" title="4.4 Sections" rel="next">
348: </head><body onload="fixBrokenLink(); init()"><div class="head" id="head">
349: <p><a href="http://www.w3.org/"><img alt="W3C" height="48" src="http://www.w3.org/Icons/w3c_home" width="72"></a></p>
1.3 mike 350:
1.1 mike 351: <h1>HTML5</h1>
352: </div><div>
1.35 mike 353: <a href="semantics.html" class="prev">4 The elements of HTML</a> –
1.1 mike 354: <a href="spec.html#contents">Table of contents</a> –
1.35 mike 355: <a href="sections.html" class="next">4.4 Sections</a>
1.1 mike 356: <ol class="toc"><li><ol><li><a href="scripting-1.html#scripting-1"><span class="secno">4.3 </span>Scripting</a>
357: <ol><li><a href="scripting-1.html#script"><span class="secno">4.3.1 </span>The <code>script</code> element</a>
358: <ol><li><a href="scripting-1.html#scriptingLanguages"><span class="secno">4.3.1.1 </span>Scripting languages</a></li><li><a href="scripting-1.html#restrictions-for-contents-of-script-elements"><span class="secno">4.3.1.2 </span>Restrictions for contents of <code>script</code> elements</a></li><li><a href="scripting-1.html#inline-documentation-for-external-scripts"><span class="secno">4.3.1.3 </span>Inline documentation for external scripts</a></li></ol></li><li><a href="scripting-1.html#the-noscript-element"><span class="secno">4.3.2 </span>The <code>noscript</code> element</a></li></ol></li></ol></li></ol></div>
359:
1.15 mike 360: <h3 id="scripting-1"><span class="secno">4.3 </span>Scripting</h3><p>Scripts allow authors to add interactivity to their documents.</p><p>Authors are encouraged to use declarative alternatives to
1.1 mike 361: scripting where possible, as declarative mechanisms are often more
362: maintainable, and many users disable scripting.</p><div class="example">
363: <p>For example, instead of using script to show or hide a section
364: to show more details, the <code><a href="interactive-elements.html#the-details-element">details</a></code> element could be
365: used.</p>
366: </div><p>Authors are also encouraged to make their applications degrade
367: gracefully in the absence of scripting support.</p><div class="example">
368: <p>For example, if an author provides a link in a table header to
369: dynamically resort the table, the link could also be made to
370: function without scripts by requesting the sorted table from the
371: server.</p>
1.15 mike 372: </div><h4 id="script"><span class="secno">4.3.1 </span>The <dfn><code>script</code></dfn> element</h4><dl class="element"><dt>Categories</dt>
1.1 mike 373: <dd><a href="content-models.html#metadata-content">Metadata content</a>.</dd>
374: <dd><a href="content-models.html#flow-content">Flow content</a>.</dd>
375: <dd><a href="content-models.html#phrasing-content">Phrasing content</a>.</dd>
1.16 mike 376: <dt>Contexts in which this element can be used:</dt>
1.1 mike 377: <dd>Where <a href="content-models.html#metadata-content">metadata content</a> is expected.</dd>
378: <dd>Where <a href="content-models.html#phrasing-content">phrasing content</a> is expected.</dd>
379: <dt>Content model:</dt>
380: <dd>If there is no <code title="attr-script-src"><a href="#attr-script-src">src</a></code>
381: attribute, depends on the value of the <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute, but must match
382: <a href="#restrictions-for-contents-of-script-elements">script content restrictions</a>.</dd>
383: <dd>If there <em>is</em> a <code title="attr-script-src"><a href="#attr-script-src">src</a></code>
384: attribute, the element must be either empty or contain only
385: <a href="#inline-documentation-for-external-scripts">script documentation</a> that also matches <a href="#restrictions-for-contents-of-script-elements">script
386: content restrictions</a>.</dd>
387: <dt>Content attributes:</dt>
388: <dd><a href="elements.html#global-attributes">Global attributes</a></dd>
389: <dd><code title="attr-script-src"><a href="#attr-script-src">src</a></code></dd>
390: <dd><code title="attr-script-async"><a href="#attr-script-async">async</a></code></dd>
391: <dd><code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code></dd>
392: <dd><code title="attr-script-type"><a href="#attr-script-type">type</a></code></dd>
393: <dd><code title="attr-script-charset"><a href="#attr-script-charset">charset</a></code></dd>
394: <dt>DOM interface:</dt>
395: <dd>
396: <pre class="idl">interface <dfn id="htmlscriptelement">HTMLScriptElement</dfn> : <a href="elements.html#htmlelement">HTMLElement</a> {
397: attribute DOMString <a href="#dom-script-src" title="dom-script-src">src</a>;
398: attribute boolean <a href="#dom-script-async" title="dom-script-async">async</a>;
399: attribute boolean <a href="#dom-script-defer" title="dom-script-defer">defer</a>;
400: attribute DOMString <a href="#dom-script-type" title="dom-script-type">type</a>;
401: attribute DOMString <a href="#dom-script-charset" title="dom-script-charset">charset</a>;
402: attribute DOMString <a href="#dom-script-text" title="dom-script-text">text</a>;
403: };</pre>
404: </dd>
405: </dl><p>The <code><a href="#script">script</a></code> element allows authors to include dynamic
406: script and data blocks in their documents. The element does not
407: <a href="rendering.html#represents" title="represents">represent</a> content for the user.</p><p>When used to include dynamic scripts, the scripts may either be
408: embedded inline or may be imported from an external file using the
409: <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute. If the language
410: is not that described by "<code title="">text/javascript</code>",
411: then the <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute must
412: be present, as described below.</p><p>When used to include data blocks (as opposed to scripts), the
413: data must be embedded inline, the format of the data must be given
414: using the <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute, and
415: the <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute must not be
416: specified.</p><p>The <dfn id="attr-script-type" title="attr-script-type"><code>type</code></dfn>
417: attribute gives the language of the script or format of the data. If
418: the attribute is present, its value must be a <a href="infrastructure.html#valid-mime-type">valid MIME
419: type</a>. The <code title="">charset</code> parameter must not be
420: specified. The default, which is used if the attribute is absent,
421: is "<code title="">text/javascript</code>".</p><p>The <dfn id="attr-script-src" title="attr-script-src"><code>src</code></dfn>
422: attribute, if specified, gives the address of the external script
423: resource to use. The value of the attribute must be a <a href="urls.html#valid-non-empty-url-potentially-surrounded-by-spaces">valid
424: non-empty URL potentially surrounded by spaces</a> identifying a
425: script resource of the type given by the <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute, if the attribute is
426: present, or of the type "<code title="">text/javascript</code>", if
427: the attribute is absent. A resource is a script resource of a given
428: type if that type identifies a scripting language and the resource
429: conforms with the requirements of that language's specification.</p><p>The <dfn id="attr-script-charset" title="attr-script-charset"><code>charset</code></dfn>
430: attribute gives the character encoding of the external script
431: resource. The attribute must not be specified if the <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute is not present. If the
432: attribute is set, its value must be a valid character encoding name,
433: must be an <a href="infrastructure.html#ascii-case-insensitive">ASCII case-insensitive</a> match for the
434: <a href="infrastructure.html#preferred-mime-name">preferred MIME name</a> for that encoding, and must match
435: the encoding given in the <code title="">charset</code> parameter of
436: the <a href="fetching-resources.html#content-type" title="Content-Type">Content-Type metadata</a> of the
437: external file, if any. <a href="references.html#refsIANACHARSET">[IANACHARSET]</a></p><p>The <dfn id="attr-script-async" title="attr-script-async"><code>async</code></dfn> and
438: <dfn id="attr-script-defer" title="attr-script-defer"><code>defer</code></dfn> attributes
439: are <a href="common-microsyntaxes.html#boolean-attribute" title="boolean attribute">boolean attributes</a> that
440: indicate how the script should be executed. The <code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code> and <code title="attr-script-async"><a href="#attr-script-async">async</a></code> attributes must not be
441: specified if the <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute
442: is not present.</p><p>There are three possible modes that can be selected using these
443: attributes. If the <code title="attr-script-async"><a href="#attr-script-async">async</a></code>
444: attribute is present, then the script will be executed
445: asynchronously, as soon as it is available. If the <code title="attr-script-async"><a href="#attr-script-async">async</a></code> attribute is not present but
446: the <code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code> attribute is
447: present, then the script is executed when the page has finished
448: parsing. If neither attribute is present, then the script is
449: fetched and executed immediately, before the user agent continues
450: parsing the page.</p><p class="note">The exact processing details for these attributes
451: are, for mostly historical reasons, somewhat non-trivial, involving
452: a number of aspects of HTML. The implementation requirements are
453: therefore by necessity scattered throughout the specification. The
454: algorithms below (in this section) describe the core of this
455: processing, but these algorithms reference and are referenced by the
456: parsing rules for <code><a href="#script">script</a></code> <a href="tokenization.html#scriptTag">start</a>
457: and <a href="tokenization.html#scriptEndTag">end</a> tags in HTML, <a href="tokenization.html#scriptForeignEndTag">in foreign content</a>, and <a href="the-xhtml-syntax.html#scriptTagXML">in XML</a>, the rules for the <code title="dom-document-write"><a href="apis-in-html-documents.html#dom-document-write">document.write()</a></code> method, the
458: handling of <a href="webappapis.html#scripting">scripting</a>, etc.</p><p>The <code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code> attribute may be
459: specified even if the <code title="attr-script-async"><a href="#attr-script-async">async</a></code>
460: attribute is specified, to cause legacy Web browsers that only
461: support <code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code> (and not <code title="attr-script-async"><a href="#attr-script-async">async</a></code>) to fall back to the <code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code> behavior instead of the
462: synchronous blocking behavior that is the default.</p><p>Changing the <code title="attr-script-src"><a href="#attr-script-src">src</a></code>, <code title="attr-script-type"><a href="#attr-script-type">type</a></code>, <code title="attr-script-charset"><a href="#attr-script-charset">charset</a></code>, <code title="attr-script-async"><a href="#attr-script-async">async</a></code>, and <code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code> attributes dynamically has no
463: direct effect; these attribute are only used at specific times
464: described below.</p><!-- by implication, changes to the base URL
465: also have no effect --><div class="impl">
466:
467: <p>A <code><a href="#script">script</a></code> element has several associated pieces of
468: state.</p>
469:
470: <p>The first is a flag indicating whether or not the script block
471: has been <dfn id="already-started">"already started"</dfn>. Initially,
472: <code><a href="#script">script</a></code> elements must have this flag unset (script
473: blocks, when created, are not "already started"). When a
1.40 ! mike 474: <code><a href="#script">script</a></code> element is <a href="infrastructure.html#concept-clone" title="concept-clone">cloned</a>, the "already started" flag, if
! 475: set, must be propagated to the clone when it is created.</p>
1.1 mike 476:
477: <p>The second is a flag indicating whether the element was
478: <dfn id="parser-inserted">"parser-inserted"</dfn>. Initially, <code><a href="#script">script</a></code>
479: elements must have this flag unset. It is set by the <a href="parsing.html#html-parser">HTML
480: parser</a> and the <a href="the-xhtml-syntax.html#xml-parser">XML parser</a> on <code><a href="#script">script</a></code>
481: elements they insert and affects the processing of those
482: elements.</p>
483:
1.39 mike 484: <p>The third is a flag indicating whether the element will
485: <dfn id="force-async">"force-async"</dfn>. Initially, <code><a href="#script">script</a></code> elements
486: must have this flag set. It is unset by the <a href="parsing.html#html-parser">HTML parser</a>
487: and the <a href="the-xhtml-syntax.html#xml-parser">XML parser</a> on <code><a href="#script">script</a></code> elements they
488: insert. In addition, whenever a <code><a href="#script">script</a></code> element whose
489: <a href="#force-async">"force-async"</a> flag is set has a <code title="attr-script-async"><a href="#attr-script-async">async</a></code> content attribute added, the
490: element's <a href="#force-async">"force-async"</a> flag must be unset.</p> <!--
491: there's no need to unset it when the attribute is removed since you
492: can't have the attribute and the flag set at once. -->
493:
494: <p>The fourth is a flag indicating whether or not the script block is
1.1 mike 495: <dfn id="ready-to-be-parser-executed">"ready to be parser-executed"</dfn>. Initially,
496: <code><a href="#script">script</a></code> elements must have this flag unset (script
497: blocks, when created, are not "ready to be parser-executed"). This
498: flag is used only for elements that are also
499: <a href="#parser-inserted">"parser-inserted"</a>, to let the parser know when to
500: execute the script.</p>
501:
1.23 mike 502: <p>The last few pieces of state are <dfn id="the-script-block-s-type"><var>the script block's
503: type</var></dfn>, <dfn id="the-script-block-s-character-encoding"><var>the script block's character
504: encoding</var></dfn>, and <dfn id="the-script-block-s-fallback-character-encoding"><var>the script block's fallback
505: character encoding</var></dfn>. They are determined when the script
1.40 ! mike 506: is prepared, based on the attributes on the element at that time,
! 507: and the <code><a href="infrastructure.html#document">Document</a></code> of the <code><a href="#script">script</a></code>
! 508: element.</p>
1.1 mike 509:
1.16 mike 510: <p>When a <code><a href="#script">script</a></code> element that is not marked as being
1.1 mike 511: <a href="#parser-inserted">"parser-inserted"</a> experiences one of the events listed
1.40 ! mike 512: in the following list, the user agent must synchronously <a href="#prepare-a-script" title="prepare a script">prepare</a> the <code><a href="#script">script</a></code>
1.1 mike 513: element:</p>
514:
515: <ul><li>The <code><a href="#script">script</a></code> element gets <a href="infrastructure.html#insert-an-element-into-a-document" title="insert an
516: element into a document">inserted into a document</a>.</li>
517:
518: <li>The <code><a href="#script">script</a></code> element is <a href="infrastructure.html#in-a-document">in a
519: <code>Document</code></a> and its child nodes are changed.</li>
520:
521: <li>The <code><a href="#script">script</a></code> element is <a href="infrastructure.html#in-a-document">in a
522: <code>Document</code></a> and has a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute set where previously
523: the element had no such attribute.</li>
524:
1.40 ! mike 525: </ul><p>To <dfn id="prepare-a-script" title="prepare a script">prepare a script</dfn>, the user
! 526: agent must act as follows:</p>
1.1 mike 527:
1.19 mike 528: <ol><li>
1.16 mike 529:
1.19 mike 530: <p>If the <code><a href="#script">script</a></code> element is marked as having
531: <a href="#already-started">"already started"</a>, then the user agent must abort
532: these steps at this point. The script is not executed.</p>
1.16 mike 533:
534: </li>
535:
1.18 mike 536: <li>
537:
538: <p>If the element has its <a href="#parser-inserted">"parser-inserted"</a> flag set,
539: then set <var title="">was-parser-inserted</var> to true and unset
540: the element's <a href="#parser-inserted">"parser-inserted"</a> flag. Otherwise, set
541: <var title="">was-parser-inserted</var> to false.</p>
542:
543: <p class="note">This is done so that if parser-inserted
544: <code><a href="#script">script</a></code> elements fail to run when the parser tries to
545: run them, e.g. because they are empty or specify an unsupported
546: scripting language, another script can later mutate them and cause
547: them to run again.</p>
548:
549: <!-- q.v. http://hsivonen.iki.fi/test/moz/script-setting-src-on-parser-inserted-script.html -->
550:
551: </li>
552:
1.39 mike 553: <li>
554:
555: <p>If <var title="">was-parser-inserted</var> is true and the
556: element does not have an <code title="attr-script-async"><a href="#attr-script-async">async</a></code> attribute, then set the
557: element's <a href="#force-async">"force-async"</a> flag to true.</p>
558:
559: <p class="note">This is done so that if a parser-inserted
560: <code><a href="#script">script</a></code> element fails to run when the parser tries to
561: run it, but it is later executed after a script dynamically
562: updates it, it will execute asynchronously even if the
563: <code title="attr-script-async"><a href="#attr-script-async">async</a></code> attribute isn't
564: set.</p>
565:
566: </li>
567:
1.16 mike 568: <li id="script-processing-empty">
569:
570: <p>If the element has no <code title="attr-script-src"><a href="#attr-script-src">src</a></code>
1.18 mike 571: attribute, and its child nodes, if any, consist only of comment
572: nodes and empty <a href="infrastructure.html#text-node" title="text node">text nodes</a>, then
573: the user agent must abort these steps at this point. The script is
574: not executed.</p>
1.16 mike 575:
576: </li>
577:
1.25 mike 578: <li>
579:
580: <p>If the element is not <a href="infrastructure.html#in-a-document">in a <code>Document</code></a>,
581: then the user agent must abort these steps at this point. The
582: script is not executed.</p>
583:
584: </li>
585:
1.16 mike 586: <li id="script-processing-prepare">
1.1 mike 587:
588: <p>If either:</p>
589:
590: <ul class="brief"><li>the <code><a href="#script">script</a></code> element has a <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute and its value is
591: the empty string, or</li>
592:
593: <li>the <code><a href="#script">script</a></code> element has no <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute but it has a <code title="attr-script-language"><a href="obsolete.html#attr-script-language">language</a></code> attribute and
594: <em>that</em> attribute's value is the empty string, or</li>
595:
596: <li>the <code><a href="#script">script</a></code> element has neither a <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute nor a <code title="attr-script-language"><a href="obsolete.html#attr-script-language">language</a></code> attribute, then</li>
597:
598: </ul><p>...let <var><a href="#the-script-block-s-type">the script block's type</a></var> for this
599: <code><a href="#script">script</a></code> element be "<code title="">text/javascript</code>".</p>
600:
601: <p>Otherwise, if the <code><a href="#script">script</a></code> element has a <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute, let <var><a href="#the-script-block-s-type">the
602: script block's type</a></var> for this <code><a href="#script">script</a></code> element be
603: the value of that attribute with any leading or trailing sequences
604: of <a href="common-microsyntaxes.html#space-character" title="space character">space characters</a>
605: removed.</p>
606:
607: <p>Otherwise, the element has a non-empty <code title="attr-script-language"><a href="obsolete.html#attr-script-language">language</a></code> attribute; let
608: <var><a href="#the-script-block-s-type">the script block's type</a></var> for this <code><a href="#script">script</a></code>
609: element be the concatenation of the string "<code title="">text/</code>" followed by the value of the <code title="attr-script-language"><a href="obsolete.html#attr-script-language">language</a></code> attribute.</p> <!--
610: user agents already support, e.g., type="text/javascript1.3", so
611: we don't have to support that separately. -->
612:
613: <p class="note">The <code title="attr-script-language"><a href="obsolete.html#attr-script-language">language</a></code> attribute is never
614: conforming, and is always ignored if there is a <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute present.</p>
615:
616: </li>
617:
1.16 mike 618: <li>
619:
620: <p>If the user agent does not <a href="#support-the-scripting-language">support the scripting
621: language</a> given by <var><a href="#the-script-block-s-type">the script block's type</a></var> for
622: this <code><a href="#script">script</a></code> element, then the user agent must abort
623: these steps at this point. The script is not executed.</p>
624:
625: </li>
626:
1.39 mike 627: <li>
1.18 mike 628:
1.39 mike 629: <p>If <var title="">was-parser-inserted</var> is true, then flag
630: the element as <a href="#parser-inserted">"parser-inserted"</a> again, and set the
631: element's <a href="#force-async">"force-async"</a> flag to false.</p>
1.18 mike 632:
633: </li>
634:
1.39 mike 635: <li id="script-processing-start">
1.18 mike 636:
1.39 mike 637: <p>The user agent must set the element's <a href="#already-started">"already
638: started"</a> flag.</p>
1.18 mike 639:
1.40 ! mike 640: <p class="note">The state of the element at this moment <a href="#establish-script-block-source">is later used</a> to
! 641: determine the script source.</p>
! 642:
! 643: </li>
! 644:
! 645: <li>
! 646:
! 647: <p>If the element is flagged as <a href="#parser-inserted">"parser-inserted"</a>,
! 648: but the element's <code><a href="infrastructure.html#document">Document</a></code> is not the
! 649: <code><a href="infrastructure.html#document">Document</a></code> of the parser that created the element, then
! 650: abort these steps.</p>
! 651:
1.18 mike 652: </li>
653:
1.19 mike 654: <li id="script-processing-noscript">
655:
656: <p>If <a href="webappapis.html#concept-n-noscript" title="concept-n-noscript">scripting is
657: disabled</a> for the <code><a href="#script">script</a></code> element, then the user
658: agent must abort these steps at this point. The script is not
659: executed.</p>
660:
1.40 ! mike 661: <p class="note">The definition of <a href="webappapis.html#concept-n-noscript" title="concept-n-noscript">scripting is disabled</a> means
! 662: that, amongst others, the following scripts will not execute:
! 663: scripts in <code>XMLHttpRequest</code>'s <code title="dom-XMLHttpRequest-responseXML">responseXML</code>
! 664: documents, scripts in <code>DOMParser</code>-created
! 665: documents, <!--scripts in documents created by
! 666: <code>XSLTProcessor</code>'s <code
! 667: title="dom-XSLTProcessor-transformToDocument">transformToDocument</code>
! 668: feature,--> and scripts that are first inserted by a script into a
! 669: <code><a href="infrastructure.html#document">Document</a></code> that was created using the <code title="dom-DOMImplementation-createDocument"><a href="infrastructure.html#dom-domimplementation-createdocument">createDocument()</a></code>
! 670: API. <a href="references.html#refsXHR">[XHR]</a> <a href="references.html#refsDOMPARSER">[DOMPARSER]</a> <!--<a
! 671: href="#refsXSLTP">[XSLTP]</a>--> <a href="references.html#refsDOMCORE">[DOMCORE]</a></p>
! 672:
! 673:
1.19 mike 674: </li>
675:
1.1 mike 676: <li id="script-processing-for">
677:
678: <p>If the <code><a href="#script">script</a></code> element has an <code title="attr-script-event"><a href="obsolete.html#attr-script-event">event</a></code> attribute and a <code title="attr-script-for"><a href="obsolete.html#attr-script-for">for</a></code> attribute, then run these
679: substeps:</p>
680:
681: <ol><li><p>Let <var title="">for</var> be the value of the <code title="attr-script-for"><a href="obsolete.html#attr-script-for">for</a></code> attribute.</p></li>
682:
683: <li><p>Let <var title="">event</var> be the value of the <code title="attr-script-event"><a href="obsolete.html#attr-script-event">event</a></code> attribute.</p></li>
684:
685: <li><p><a href="common-microsyntaxes.html#strip-leading-and-trailing-whitespace">Strip leading and trailing whitespace</a> from
686: <var title="">event</var> and <var title="">for</var>.</p></li>
687:
688: <li><p>If <var title="">for</var> is not an <a href="infrastructure.html#ascii-case-insensitive">ASCII
689: case-insensitive</a> match for the string "<code title="">window</code>", then the user agent must abort these
690: steps at this point. The script is not executed.</p></li>
691:
692: <li><p>If <var title="">event</var> is not an <a href="infrastructure.html#ascii-case-insensitive">ASCII
693: case-insensitive</a> match for either the string "<code title="">onload</code>" or the string "<code title="">onload()</code>", then the user agent must abort these
694: steps at this point. The script is not executed.</p></li>
695:
696: </ol></li>
697:
1.19 mike 698: <li id="script-processing-encoding">
699:
700: <p>If the <code><a href="#script">script</a></code> element has a <code title="attr-script-charset"><a href="#attr-script-charset">charset</a></code> attribute, then let
701: <var><a href="#the-script-block-s-character-encoding">the script block's character encoding</a></var> for this
702: <code><a href="#script">script</a></code> element be the encoding given by the <code title="attr-script-charset"><a href="#attr-script-charset">charset</a></code> attribute.</p>
703:
1.23 mike 704: <p>Otherwise, let <var><a href="#the-script-block-s-fallback-character-encoding">the script block's fallback character
705: encoding</a></var> for this <code><a href="#script">script</a></code> element be the same as
706: <a href="dom.html#document-s-character-encoding" title="document's character encoding">the encoding of the
707: document itself</a>.</p>
708:
709: <p class="note">Only one of these two pieces of state is set.</p>
1.19 mike 710:
711: </li>
712:
1.1 mike 713: <li id="script-processing-src-prepare">
714:
715: <p>If the element has a <code title="attr-script-src"><a href="#attr-script-src">src</a></code>
716: attribute whose value is not the empty string, then the value of
717: that attribute must be <a href="urls.html#resolve-a-url" title="resolve a url">resolved</a>
718: relative to the element, and if that is successful, the specified
719: resource must then be <a href="fetching-resources.html#fetch" title="fetch">fetched</a>, from the
720: <a href="origin-0.html#origin">origin</a> of the element's <code><a href="infrastructure.html#document">Document</a></code>.</p>
721: <!-- not http-origin privacy sensitive -->
722:
723: <p>If the <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute's
724: value is the empty string or if it could not be resolved, then the
725: user agent must <a href="webappapis.html#queue-a-task">queue a task</a> to <a href="webappapis.html#fire-a-simple-event">fire a simple
726: event</a> named <code title="event-error">error</code> at the
727: element, and abort these steps.</p>
728:
729: <p>For historical reasons, if the <a href="urls.html#url">URL</a> is a <a href="webappapis.html#javascript-protocol" title="javascript protocol"><code title="">javascript:</code>
730: URL</a>, then the user agent must not, despite the requirements
731: in the definition of the <a href="fetching-resources.html#fetch" title="fetch">fetching</a>
732: algorithm, actually execute the script in the URL; instead the
733: user agent must act as if it had received an empty HTTP 400
734: response.</p>
735:
736: <p>For performance reasons, user agents may start fetching the
737: script as soon as the attribute is set, instead, in the hope that
738: the element will be inserted into the document. Either way, once
739: the element is <a href="infrastructure.html#insert-an-element-into-a-document" title="insert an element into a
740: document">inserted into the document</a>, the load must have
741: started. If the UA performs such prefetching, but the element is
742: never inserted in the document, or the <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute is dynamically
743: changed,<!-- or the base URL is dynamically changed,--> then the
744: user agent will not execute the script, and the fetching process
745: will have been effectively wasted.</p>
746:
747: </li>
748:
1.39 mike 749: <!-- at this point if the element is "parser-inserted" then we know
750: it is not "force-async". -->
751:
1.1 mike 752: <li>
753:
754: <p>Then, the first of the following options that describes the
755: situation must be followed:</p>
756:
757: <dl class="switch"><dt id="script-processing-defer">If the element has a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute, and the element has
758: a <code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code> attribute, and the
759: element has been flagged as <a href="#parser-inserted">"parser-inserted"</a>, and
760: the element does not have an <code title="attr-script-async"><a href="#attr-script-async">async</a></code> attribute</dt>
761:
762: <dd>
763:
764: <p>The element must be added to the end of the <dfn id="list-of-scripts-that-will-execute-when-the-document-has-finished-parsing">list of
765: scripts that will execute when the document has finished
1.20 mike 766: parsing</dfn> associated with the <code><a href="infrastructure.html#document">Document</a></code> of the
767: parser that created the element.</p>
1.1 mike 768:
769: <p>The <a href="webappapis.html#concept-task" title="concept-task">task</a> that the
770: <a href="webappapis.html#networking-task-source">networking task source</a> places on the <a href="webappapis.html#task-queue">task
771: queue</a> once the <a href="fetching-resources.html#fetch" title="fetch">fetching
772: algorithm</a> has completed must set the element's
773: <a href="#ready-to-be-parser-executed">"ready to be parser-executed"</a> flag. The parser will
774: handle executing the script.</p>
775:
776: </dd>
777:
778:
779: <dt id="script-processing-parser-inserted">If the element has a
780: <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute, and the
781: element has been flagged as <a href="#parser-inserted">"parser-inserted"</a>, and
782: the element does not have an <code title="attr-script-async"><a href="#attr-script-async">async</a></code> attribute</dt>
783:
784: <dd>
785:
786: <p>The element is the <a href="#pending-parsing-blocking-script">pending parsing-blocking
1.20 mike 787: script</a> of the <code><a href="infrastructure.html#document">Document</a></code> of the parser that
788: created the element. (There can only be one such script per
789: <code><a href="infrastructure.html#document">Document</a></code> at a time.)</p>
1.1 mike 790:
791: <p>The <a href="webappapis.html#concept-task" title="concept-task">task</a> that the
792: <a href="webappapis.html#networking-task-source">networking task source</a> places on the <a href="webappapis.html#task-queue">task
793: queue</a> once the <a href="fetching-resources.html#fetch" title="fetch">fetching
794: algorithm</a> has completed must set the element's
795: <a href="#ready-to-be-parser-executed">"ready to be parser-executed"</a> flag. The parser will
796: handle executing the script.</p>
797:
798: </dd>
799:
800:
801: <dt id="script-processing-style-delayed">If the element does not
1.33 mike 802: have a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute, and
803: the element has been flagged as <a href="#parser-inserted">"parser-inserted"</a>,
804: and the <code><a href="infrastructure.html#document">Document</a></code> of the <a href="parsing.html#html-parser">HTML parser</a> or
805: <a href="the-xhtml-syntax.html#xml-parser">XML parser</a> that created the <code><a href="#script">script</a></code>
806: element <a href="semantics.html#has-a-style-sheet-that-is-blocking-scripts">has a style sheet that is blocking
807: scripts</a></dt>
1.1 mike 808:
809: <dd>
810:
811: <p>The element is the <a href="#pending-parsing-blocking-script">pending parsing-blocking
1.20 mike 812: script</a> of the <code><a href="infrastructure.html#document">Document</a></code> of the parser that
813: created the element. (There can only be one such script per
814: <code><a href="infrastructure.html#document">Document</a></code> at a time.)</p>
1.1 mike 815:
816: <p>Set the element's <a href="#ready-to-be-parser-executed">"ready to be parser-executed"</a>
817: flag. The parser will handle executing the script.</p>
818:
819: </dd>
820:
821:
1.39 mike 822: <dt id="script-processing-src-sync">If the element has a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute, does not have an
823: <code title="attr-script-async"><a href="#attr-script-async">async</a></code> attribute, and does
824: not have the <a href="#force-async">"force-async"</a> flag set</dt>
825:
826: <dd>
827:
828: <p>The element must be added to the end of the <dfn id="list-of-scripts-that-will-execute-in-order-as-soon-as-possible">list of
829: scripts that will execute in order as soon as possible</dfn>
830: associated with the <code><a href="infrastructure.html#document">Document</a></code> of the
1.40 ! mike 831: <code><a href="#script">script</a></code> element at the time the <a href="#prepare-a-script">prepare a
1.39 mike 832: script</a> algorithm started.</p>
833:
834: <p>The <a href="webappapis.html#concept-task" title="concept-task">task</a> that the
835: <a href="webappapis.html#networking-task-source">networking task source</a> places on the <a href="webappapis.html#task-queue">task
836: queue</a> once the <a href="fetching-resources.html#fetch" title="fetch">fetching
837: algorithm</a> has completed must run the following steps:</p>
838:
839: <ol><li><p>If the element is not now the first element in the
840: <a href="#list-of-scripts-that-will-execute-in-order-as-soon-as-possible">list of scripts that will execute in order as soon as
841: possible</a> to which it was added above, then mark the
842: element as ready but abort these steps without executing the
843: script yet.</p></li>
844:
1.40 ! mike 845: <li><p><i>Execution</i>: <a href="#execute-the-script-block">Execute the script block</a>
! 846: corresponding to the first script element in this <a href="#list-of-scripts-that-will-execute-in-order-as-soon-as-possible">list of
! 847: scripts that will execute in order as soon as
! 848: possible</a>.</p></li>
1.39 mike 849:
850: <li><p>Remove the first element from this <a href="#list-of-scripts-that-will-execute-in-order-as-soon-as-possible">list of scripts
851: that will execute in order as soon as possible</a>.</p></li>
852:
853: <li><p>If this <a href="#list-of-scripts-that-will-execute-in-order-as-soon-as-possible">list of scripts that will execute in order
854: as soon as possible</a> is still not empty and the first
855: entry has already been marked as ready, then jump back to the
856: step labeled <i>execution</i>.</p></li>
857:
858: </ol></dd>
859:
860:
1.1 mike 861: <dt id="script-processing-src">If the element has a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute</dt>
862:
863: <dd>
864:
865: <p>The element must be added to the <dfn id="set-of-scripts-that-will-execute-as-soon-as-possible">set of scripts that
1.20 mike 866: will execute as soon as possible</dfn> of the
867: <code><a href="infrastructure.html#document">Document</a></code> of the <code><a href="#script">script</a></code> element at the
1.40 ! mike 868: time the <a href="#prepare-a-script">prepare a script</a> algorithm started.</p>
1.1 mike 869:
870: <p>The <a href="webappapis.html#concept-task" title="concept-task">task</a> that the
871: <a href="webappapis.html#networking-task-source">networking task source</a> places on the <a href="webappapis.html#task-queue">task
872: queue</a> once the <a href="fetching-resources.html#fetch" title="fetch">fetching
1.40 ! mike 873: algorithm</a> has completed must <a href="#execute-the-script-block">execute the script
! 874: block</a> and then remove the element from the <a href="#set-of-scripts-that-will-execute-as-soon-as-possible">set of
! 875: scripts that will execute as soon as possible</a>.</p>
1.1 mike 876:
877: </dd>
878:
879:
880: <dt id="script-processing-inline">Otherwise</dt>
881:
1.40 ! mike 882: <dd>The user agent must immediately <a href="#execute-the-script-block">execute the script
! 883: block</a>, even if other scripts are already executing.</dd>
1.1 mike 884:
885: </dl></li>
886:
887: </ol><!-- similar text in various places --><p>Fetching an external script must <a href="the-end.html#delay-the-load-event">delay the load
888: event</a> of the element's document until the <a href="webappapis.html#concept-task" title="concept-task">task</a> that is <a href="webappapis.html#queue-a-task" title="queue a
889: task">queued</a> by the <a href="webappapis.html#networking-task-source">networking task source</a> once
890: the resource has been <a href="fetching-resources.html#fetch" title="fetch">fetched</a> (defined
891: above) has been run.</p>
892:
1.20 mike 893: <p>The <dfn id="pending-parsing-blocking-script">pending parsing-blocking script</dfn> of a
894: <code><a href="infrastructure.html#document">Document</a></code> is used by the <code><a href="infrastructure.html#document">Document</a></code>'s
895: parser(s).</p>
1.1 mike 896:
1.26 mike 897: <p class="note">If a <code><a href="#script">script</a></code> element that blocks a
898: parser gets moved to another <code><a href="infrastructure.html#document">Document</a></code> before it would
899: normally have stopped blocking that parser, it nonetheless continues
900: blocking that parser until the condition that causes it to be
901: blocking the parser no longer applies (e.g. if the script is a
902: <a href="#pending-parsing-blocking-script">pending parsing-blocking script</a> because there was
903: <a href="semantics.html#a-style-sheet-that-is-blocking-scripts">a style sheet that is blocking scripts</a> when it was
904: parsed, but then the script is moved to another
905: <code><a href="infrastructure.html#document">Document</a></code> before the style sheet loads, the script still
906: blocks the parser until the style sheets are all loaded, at which
907: time the script executes and the parser is unblocked).</p>
908:
909: <!-- also (and this would be worth testing): the way the spec is
910: written, if you do not have a style sheet that is blocking scripts,
911: then you parse a <script src>, then while waiting for the script to
912: load you insert an external style sheet, the script will delay until
913: the sheet is loaded, because there's just a binary "are style sheets
914: blocking scripts" state, things aren't defined in terms of which
915: style sheets are blocking which scripts -->
916:
1.40 ! mike 917: <p>When the user agent is required to <dfn id="execute-the-script-block" title="execute the script
! 918: block">execute a script block</dfn>, it must run the following
! 919: steps:</p>
! 920:
! 921: <ol><li>
! 922:
! 923: <p>If the element is flagged as <a href="#parser-inserted">"parser-inserted"</a>,
! 924: but the element's <code><a href="infrastructure.html#document">Document</a></code> is not the
! 925: <code><a href="infrastructure.html#document">Document</a></code> of the parser that created the element, then
! 926: abort these steps.</p>
1.1 mike 927:
1.40 ! mike 928: </li>
! 929:
! 930: <li>
1.1 mike 931:
1.40 ! mike 932: <p>Jump to the appropriate set of steps from the list below:</p>
1.1 mike 933:
1.40 ! mike 934: <dl class="switch"><dt>If the load resulted in an error (for example a DNS error, or
! 935: an HTTP 404 error)</dt>
1.1 mike 936:
1.40 ! mike 937: <dd><p>Executing the script block must just consist of <a href="webappapis.html#fire-a-simple-event" title="fire a simple event">firing a simple event</a> named
! 938: <code title="event-error">error</code> at the element.</p></dd>
! 939:
! 940: <dt>If the load was successful</dt>
! 941:
! 942: <!-- SCRIPT EXEC -->
! 943: <dd>
! 944:
! 945: <p>Executing the script block must consists of running the
! 946: following steps. For the purposes of these steps, the script is
! 947: considered to be from an <i>external file</i> if, while the
! 948: <a href="#prepare-a-script">prepare a script</a> algorithm above was running for
! 949: this script, the <code><a href="#script">script</a></code> element had a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute specified.</p>
1.1 mike 950:
1.40 ! mike 951: <ol><li id="establish-script-block-source">
1.27 mike 952:
1.40 ! mike 953: <p>Initialize <dfn id="the-script-block-s-source"><var>the script block's source</var></dfn>
! 954: as follows:</p>
1.1 mike 955:
1.40 ! mike 956: <dl class="switch"><dt>If the script is from an external file and <var><a href="#the-script-block-s-type">the
! 957: script block's type</a></var> is a text-based language</dt>
1.1 mike 958:
1.40 ! mike 959: <dd>
1.1 mike 960:
1.40 ! mike 961: <p>The contents of that file, interpreted as string of
! 962: Unicode characters, are the script source.</p>
1.1 mike 963:
1.40 ! mike 964: <p>To obtain the string of Unicode characters, the user
! 965: agent run the following steps:</p>
1.1 mike 966:
1.40 ! mike 967: <ol><li><p>If the resource's <a href="fetching-resources.html#content-type" title="Content-Type">Content
! 968: Type metadata</a>, if any, specifies a character
! 969: encoding, and the user agent supports that encoding, then
! 970: let <var title="">character encoding</var> be that
! 971: encoding, and jump to the bottom step in this series of
! 972: steps.</p></li>
1.1 mike 973:
1.40 ! mike 974: <li><p>If the algorithm above set <var><a href="#the-script-block-s-character-encoding">the script block's
! 975: character encoding</a></var>, then let <var title="">character
! 976: encoding</var> be that encoding, and jump to the bottom
! 977: step in this series of steps.</p></li>
1.23 mike 978:
1.40 ! mike 979: <li><p>For each of the rows in the following table,
! 980: starting with the first one and going down, if the file has
! 981: as many or more bytes available than the number of bytes in
! 982: the first column, and the first bytes of the file match the
! 983: bytes given in the first column, then set <var title="">character encoding</var> to the encoding given in
! 984: the cell in the second column of that row, and jump to the
! 985: bottom step in this series of steps:</p>
1.23 mike 986:
1.40 ! mike 987: <!-- this table is present in several forms in this file; keep them in sync -->
! 988: <table id="table-script-bom"><thead><tr><th>Bytes in Hexadecimal
! 989: </th><th>Encoding
! 990: </th></tr></thead><tbody><!-- nobody uses this
! 991: <tr>
! 992: <td>00 00 FE FF
! 993: <td>UTF-32BE
! 994: <tr>
! 995: <td>FF FE 00 00
! 996: <td>UTF-32LE
! 997: --><tr><td>FE FF
! 998: </td><td>Big-endian UTF-16
! 999: </td></tr><tr><td>FF FE
! 1000: </td><td>Little-endian UTF-16
! 1001: </td></tr><tr><td>EF BB BF
! 1002: </td><td>UTF-8
! 1003: <!-- nobody uses this
! 1004: <tr>
! 1005: <td>DD 73 66 73
! 1006: <td>UTF-EBCDIC
! 1007: -->
! 1008: </td></tr></tbody></table><p class="note">This step looks for Unicode Byte Order
! 1009: Marks (BOMs).</p>
1.23 mike 1010:
1.40 ! mike 1011: </li>
1.23 mike 1012:
1.40 ! mike 1013: <li><p>Let <var title="">character encoding</var> be
! 1014: <var><a href="#the-script-block-s-fallback-character-encoding">the script block's fallback character
! 1015: encoding</a></var>.</p></li>
1.23 mike 1016:
1.40 ! mike 1017: <li><p>Convert the file to Unicode using <var>character
! 1018: encoding</var>, following the rules for doing so given by
! 1019: the specification for <var><a href="#the-script-block-s-type">the script block's
! 1020: type</a></var>.</p></li>
1.23 mike 1021:
1.40 ! mike 1022: </ol></dd>
1.23 mike 1023:
1.40 ! mike 1024: <dt>If the script is from an external file and <var><a href="#the-script-block-s-type">the
! 1025: script block's type</a></var> is an XML-based language</dt>
1.1 mike 1026:
1.40 ! mike 1027: <dd>
1.1 mike 1028:
1.40 ! mike 1029: <p>The external file is the script source. When it is later
! 1030: executed, it must be interpreted in a manner consistent with
! 1031: the specification defining the language given by <var><a href="#the-script-block-s-type">the
! 1032: script block's type</a></var>.</p>
1.1 mike 1033:
1.40 ! mike 1034: </dd>
1.1 mike 1035:
1.40 ! mike 1036: <dt>If the script is inline and <var><a href="#the-script-block-s-type">the script block's
! 1037: type</a></var> is a text-based language</dt>
1.1 mike 1038:
1.40 ! mike 1039: <dd>
1.1 mike 1040:
1.40 ! mike 1041: <p>The value of the <code title="dom-script-text"><a href="#dom-script-text">text</a></code> IDL attribute at the
! 1042: time the element's <a href="#already-started">"already started"</a> flag was
! 1043: last set is the script source.</p>
1.1 mike 1044:
1.40 ! mike 1045: </dd>
1.1 mike 1046:
1.40 ! mike 1047: <dt>If the script is inline and <var><a href="#the-script-block-s-type">the script block's
! 1048: type</a></var> is an XML-based language</dt>
1.1 mike 1049:
1.40 ! mike 1050: <dd>
1.1 mike 1051:
1.40 ! mike 1052: <p>The child nodes of the <code><a href="#script">script</a></code> element at the
! 1053: time the element's <a href="#already-started">"already started"</a> flag was
! 1054: last set are the script source.</p>
1.1 mike 1055:
1.40 ! mike 1056: </dd>
1.1 mike 1057:
1.40 ! mike 1058: </dl></li>
1.1 mike 1059:
1.40 ! mike 1060: <li>
1.1 mike 1061:
1.40 ! mike 1062: <p>If the script is from an external file, then increment the
! 1063: <a href="apis-in-html-documents.html#ignore-destructive-writes-counter">ignore-destructive-writes counter</a> of the
! 1064: <code><a href="#script">script</a></code> element's <code><a href="infrastructure.html#document">Document</a></code>. Let <var title="">neutralized doc</var> be that
! 1065: <code><a href="infrastructure.html#document">Document</a></code>.</p>
1.1 mike 1066:
1.40 ! mike 1067: </li>
1.7 mike 1068:
1.40 ! mike 1069: <li>
1.7 mike 1070:
1.40 ! mike 1071: <p><a href="webappapis.html#create-a-script-from-a-node" title="create a script from a node">Create a
! 1072: script</a> from the <code><a href="#script">script</a></code> element node, using
! 1073: <var><a href="#the-script-block-s-source">the script block's source</a></var> and <var><a href="#the-script-block-s-type">the script
! 1074: block's type</a></var>.</p>
1.7 mike 1075:
1.40 ! mike 1076: <p class="note">This is where the script is compiled and
! 1077: actually executed.</p>
1.1 mike 1078:
1.40 ! mike 1079: </li>
1.1 mike 1080:
1.40 ! mike 1081: <li>
1.1 mike 1082:
1.40 ! mike 1083: <p>Decrement the <a href="apis-in-html-documents.html#ignore-destructive-writes-counter">ignore-destructive-writes
! 1084: counter</a> of <var title="">neutralized doc</var>, if it
! 1085: was incremented in the earlier step.</p>
1.1 mike 1086:
1.40 ! mike 1087: </li>
1.7 mike 1088:
1.40 ! mike 1089: <li>
1.7 mike 1090:
1.40 ! mike 1091: <p>If the script is from an external file, <a href="webappapis.html#fire-a-simple-event">fire a simple
! 1092: event</a> named <code title="event-load">load</code> at the
! 1093: <code><a href="#script">script</a></code> element.</p>
1.7 mike 1094:
1.40 ! mike 1095: <p>Otherwise, the script is internal; <a href="webappapis.html#queue-a-task">queue a
! 1096: task</a> to <a href="webappapis.html#fire-a-simple-event">fire a simple event</a> named <code title="event-load">load</code> at the <code><a href="#script">script</a></code>
! 1097: element.</p>
1.1 mike 1098:
1.40 ! mike 1099: </li>
1.1 mike 1100:
1.40 ! mike 1101: </ol></dd>
1.1 mike 1102:
1.40 ! mike 1103: </dl></li>
1.1 mike 1104:
1.40 ! mike 1105: </ol><p>The IDL attributes <dfn id="dom-script-src" title="dom-script-src"><code>src</code></dfn>, <dfn id="dom-script-type" title="dom-script-type"><code>type</code></dfn>, <dfn id="dom-script-charset" title="dom-script-charset"><code>charset</code></dfn>, and <dfn id="dom-script-defer" title="dom-script-defer"><code>defer</code></dfn>, each must
1.1 mike 1106: <a href="common-dom-interfaces.html#reflect">reflect</a> the respective content attributes of the same
1107: name.</p>
1108:
1.39 mike 1109: <p>The <dfn id="dom-script-async" title="dom-script-async"><code>async</code></dfn> IDL
1110: attribute controls whether the element will execute asynchronously
1111: or not. If the element's <a href="#force-async">"force-async"</a> flag is set,
1112: then, on getting, the <code title="dom-script-async"><a href="#dom-script-async">async</a></code>
1113: IDL attribute must return true, and on setting, the
1114: <a href="#force-async">"force-async"</a> flag must first be unset, and then the
1115: content attribute must be removed if the IDL attribute's new value
1116: is false, and must be set to the empty string if the IDL attribute's
1117: new value is true. If the element's <a href="#force-async">"force-async"</a> flag
1118: is <em>not</em> set, the IDL attribute must <a href="common-dom-interfaces.html#reflect">reflect</a> the
1119: <code title="attr-script-async"><a href="#attr-script-async">async</a></code> content attribute.</p>
1120:
1.1 mike 1121: </div><dl class="domintro"><dt><var title="">script</var> . <code title="dom-script-text"><a href="#dom-script-text">text</a></code> [ = <var title="">value</var> ]</dt>
1122:
1123: <dd>
1124:
1125: <p>Returns the contents of the element, ignoring child nodes that
1126: aren't <a href="infrastructure.html#text-node" title="text node">text nodes</a>.</p>
1127:
1128: <p>Can be set, to replace the element's children with the given
1129: value.</p>
1130:
1131: </dd>
1132:
1133: </dl><div class="impl">
1134:
1135: <p>The IDL attribute <dfn id="dom-script-text" title="dom-script-text"><code>text</code></dfn> must return a
1136: concatenation of the contents of all the <a href="infrastructure.html#text-node" title="text
1137: node">text nodes</a> that are direct children of the
1138: <code><a href="#script">script</a></code> element (ignoring any other nodes such as
1139: comments or elements), in tree order. On setting, it must act the
1140: same way as the <code><a href="infrastructure.html#textcontent">textContent</a></code> IDL attribute.</p>
1141:
1142: </div><p class="note">When inserted using the <code title="dom-document-write"><a href="apis-in-html-documents.html#dom-document-write">document.write()</a></code> method,
1143: <code><a href="#script">script</a></code> elements execute (typically synchronously), but
1144: when inserted using <code title="dom-innerHTML"><a href="apis-in-html-documents.html#dom-innerhtml">innerHTML</a></code> and <code title="dom-outerHTML"><a href="apis-in-html-documents.html#dom-outerhtml">outerHTML</a></code> attributes, they do not
1145: execute at all.</p><div class="example">
1146:
1147: <p>In this example, two <code><a href="#script">script</a></code> elements are used. One
1148: embeds an external script, and the other includes some data.</p>
1149:
1150: <pre><script src="game-engine.js"></script>
1151: <script type="text/x-game-map">
1152: ........U.........e
1153: o............A....e
1154: .....A.....AAA....e
1155: .A..AAA...AAAAA...e
1156: </script></pre>
1157:
1158: <p>The data in this case might be used by the script to generate
1159: the map of a video game. The data doesn't have to be used that way,
1160: though; maybe the map data is actually embedded in other parts of
1161: the page's markup, and the data block here is just used by the
1162: site's search engine to help users who are looking for particular
1163: features in their game maps.</p>
1164:
1165: </div><div class="example">
1166:
1167: <p>The following sample shows how a script element can be used to
1168: define a function that is then used by other parts of the
1169: document. It also shows how a <code><a href="#script">script</a></code> element can be
1170: used to invoke script while the document is being parsed, in this
1171: case to initialize the form's output.</p>
1172:
1173: <pre><script>
1174: function calculate(form) {
1175: var price = 52000;
1176: if (form.elements.brakes.checked)
1177: price += 1000;
1178: if (form.elements.radio.checked)
1179: price += 2500;
1180: if (form.elements.turbo.checked)
1181: price += 5000;
1182: if (form.elements.sticker.checked)
1183: price += 250;
1184: form.elements.result.value = price;
1185: }
1186: </script>
1187: <form name="pricecalc" onsubmit="return false">
1188: <fieldset>
1189: <legend>Work out the price of your car</legend>
1190: <p>Base cost: £52000.</p>
1191: <p>Select additional options:</p>
1192: <ul>
1193: <li><label><input type=checkbox name=brakes> Ceramic brakes (£1000)</label></li>
1194: <li><label><input type=checkbox name=radio> Satellite radio (£2500)</label></li>
1195: <li><label><input type=checkbox name=turbo> Turbo charger (£5000)</label></li>
1196: <li><label><input type=checkbox name=sticker> "XZ" sticker (£250)</label></li>
1197: </ul>
1198: <p>Total: £<output name=result onformchange="calculate(form)"></output></p>
1199: </fieldset>
1200: <script>
1201: document.forms.pricecalc.dispatchFormChange();
1202: </script>
1203: </form></pre>
1204:
1.15 mike 1205: </div><h5 id="scriptingLanguages"><span class="secno">4.3.1.1 </span>Scripting languages</h5><div class="impl">
1.1 mike 1206:
1207: <p>A user agent is said to <dfn id="support-the-scripting-language">support the scripting language</dfn>
1208: if <var><a href="#the-script-block-s-type">the script block's type</a></var> is an <a href="infrastructure.html#ascii-case-insensitive">ASCII
1209: case-insensitive</a> match for the <a href="infrastructure.html#mime-type">MIME type</a> string
1210: of a scripting language that the user agent implements.</p>
1211:
1212: </div><p>The following lists some <a href="infrastructure.html#mime-type">MIME type</a> strings and the
1213: languages to which they refer:</p><dl><dt>"<code>application/ecmascript</code>"</dt>
1214: <dt>"<code>application/javascript</code>"</dt>
1215: <dt>"<code>application/x-ecmascript</code>"</dt>
1216: <dt>"<code>application/x-javascript</code>"</dt>
1217: <dt>"<code>text/ecmascript</code>"</dt>
1218: <dt>"<code>text/javascript</code>"</dt>
1219: <dt>"<code>text/javascript1.0</code>"</dt>
1220: <dt>"<code>text/javascript1.1</code>"</dt>
1221: <dt>"<code>text/javascript1.2</code>"</dt>
1222: <dt>"<code>text/javascript1.3</code>"</dt>
1223: <dt>"<code>text/javascript1.4</code>"</dt>
1224: <dt>"<code>text/javascript1.5</code>"</dt>
1225: <dt>"<code>text/jscript</code>"</dt>
1226: <dt>"<code>text/livescript</code>"</dt>
1227: <dt>"<code>text/x-ecmascript</code>"</dt>
1228: <dt>"<code>text/x-javascript</code>"</dt>
1229: <dd>JavaScript. <a href="references.html#refsECMA262">[ECMA262]</a></dd>
1230:
1231: <dt>"<code>text/javascript;e4x=1</code>"</dt>
1232: <dd>JavaScript with ECMAScript for XML. <a href="references.html#refsECMA357">[ECMA357]</a></dd>
1233:
1234: </dl><div class="impl">
1235:
1236: <p>User agents may support other <a href="infrastructure.html#mime-type" title="MIME type">MIME
1237: types</a> and other languages.</p>
1238:
1239: <!-- this paragraph is also present in the <style type="text/css"> section -->
1240: <p>When examining types to determine if they support the language,
1241: user agents must not ignore unknown MIME parameters — types
1242: with unknown parameters must be assumed to be unsupported. The <code title="">charset</code> parameter must be treated as an unknown
1243: parameter for the purpose of comparing <a href="infrastructure.html#mime-type" title="MIME type">MIME
1244: types</a> here.</p>
1245:
1.37 mike 1246: </div><h5 id="restrictions-for-contents-of-script-elements"><span class="secno">4.3.1.2 </span><dfn title="script content restrictions">Restrictions for contents of <code>script</code> elements</dfn></h5><p>The <code><a href="infrastructure.html#textcontent">textContent</a></code> of a <code><a href="#script">script</a></code> element
1247: must match the <code title="">script</code> production in the
1248: following ABNF, the character set for which is Unicode. <a href="references.html#refsABNF">[ABNF]</a></p><pre>script = data1 *( escape [ script-start data3 ] "-->" data1 ) [ escape ]
1.1 mike 1249: escape = "<!--" data2 *( script-start data3 script-end data2 )
1250:
1251: data1 = <any string that doesn't contain a substring that matches not-data1>
1252: not-data1 = <!-- script-end / -->"<!--" <!-- the script-end is redundant here since it would close the element -->
1253:
1254: data2 = <any string that doesn't contain a substring that matches not-data2>
1255: not-data2 = <!-- script-end / -->script-start / "-->" <!-- the script-end is redundant here since it would close the element -->
1256:
1257: data3 = <any string that doesn't contain a substring that matches not-data3>
1258: not-data3 = script-end / "-->"
1259:
1260: script-start = lt s c r i p t tag-end
1261: script-end = lt slash s c r i p t tag-end
1262:
1263: lt = %x003C ; U+003C LESS-THAN SIGN character (<)
1264: slash = %x002F ; U+002F SOLIDUS character (/)
1265:
1266: s = %x0053 ; U+0053 LATIN CAPITAL LETTER S
1267: s =/ %x0073 ; U+0073 LATIN SMALL LETTER S
1268: c = %x0043 ; U+0043 LATIN CAPITAL LETTER C
1269: c =/ %x0063 ; U+0063 LATIN SMALL LETTER C
1270: r = %x0052 ; U+0052 LATIN CAPITAL LETTER R
1271: r =/ %x0072 ; U+0072 LATIN SMALL LETTER R
1272: i = %x0049 ; U+0049 LATIN CAPITAL LETTER I
1273: i =/ %x0069 ; U+0069 LATIN SMALL LETTER I
1274: p = %x0050 ; U+0050 LATIN CAPITAL LETTER P
1275: p =/ %x0070 ; U+0070 LATIN SMALL LETTER P
1276: t = %x0054 ; U+0054 LATIN CAPITAL LETTER T
1277: t =/ %x0074 ; U+0074 LATIN SMALL LETTER T
1278:
1279: tag-end = %x0009 ; U+0009 CHARACTER TABULATION
1280: tag-end =/ %x000A ; U+000A LINE FEED (LF)
1281: tag-end =/ %x000C ; U+000C FORM FEED (FF)
1282: tag-end =/ %x0020 ; U+0020 SPACE
1283: tag-end =/ %x002F ; U+002F SOLIDUS (/)
1284: tag-end =/ %x003E ; U+003E GREATER-THAN SIGN (>)</pre><p>When a <code><a href="#script">script</a></code> element contains <a href="#inline-documentation-for-external-scripts">script
1285: documentation</a>, there are further restrictions on the contents
1.15 mike 1286: of the element, as described in the section below.</p><h5 id="inline-documentation-for-external-scripts"><span class="secno">4.3.1.3 </span><dfn title="script documentation">Inline documentation for external scripts</dfn></h5><p>If a <code><a href="#script">script</a></code> element's <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute is specified, then the
1.1 mike 1287: contents of the <code><a href="#script">script</a></code> element, if any, must be such
1288: that the value of the <code title="dom-script-text"><a href="#dom-script-text">text</a></code> IDL
1289: attribute, which is derived from the element's contents, matches the
1290: <code title="">documentation</code> production in the following
1291: ABNF, the character set for which is Unicode. <a href="references.html#refsABNF">[ABNF]</a></p><pre>documentation = *( *( space / tab / comment ) [ line-comment ] newline )
1292: comment = slash star *( not-star / star not-slash ) 1*star slash
1293: line-comment = slash slash *not-newline
1294:
1295: ; characters
1296: tab = %x0009 ; U+0009 TAB
1297: newline = %x000A ; U+000A LINE FEED (LF)
1298: space = %x0020 ; U+0020 SPACE
1299: star = %x002A ; U+002A ASTERISK (*)
1300: slash = %x002F ; U+002F SOLIDUS (/)
1301: not-newline = %x0000-0009 / %x000B-10FFFF
1302: ; a Unicode character other than U+000A LINE FEED (LF)
1303: not-star = %x0000-0029 / %x002B-10FFFF
1304: ; a Unicode character other than U+002A ASTERISK (*)
1305: not-slash = %x0000-002E / %x0030-10FFFF
1306: ; a Unicode character other than U+002F SOLIDUS (/)</pre><p class="note">This corresponds to putting the contents of the
1307: element in JavaScript comments.</p><p class="note">This requirement is in addition to the earlier
1308: restrictions on the syntax of contents of <code><a href="#script">script</a></code>
1309: elements.</p><div class="example">
1310:
1311: <p>This allows authors to include documentation, such as license
1312: information or API information, inside their documents while still
1313: referring to external script files. The syntax is constrained so
1314: that authors don't accidentally include what looks like valid
1315: script while also providing a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute.</p>
1316:
1317: <pre><script src="cool-effects.js">
1318: // create new instances using:
1319: // var e = new Effect();
1320: // start the effect using .play, stop using .stop:
1321: // e.play();
1322: // e.stop();
1323: </script></pre>
1324:
1.22 mike 1325: </div><h4 id="the-noscript-element"><span class="secno">4.3.2 </span>The <dfn><code>noscript</code></dfn> element</h4><dl class="element"><dt>Categories</dt>
1.1 mike 1326: <dd><a href="content-models.html#metadata-content">Metadata content</a>.</dd>
1327: <dd><a href="content-models.html#flow-content">Flow content</a>.</dd>
1328: <dd><a href="content-models.html#phrasing-content">Phrasing content</a>.</dd>
1.16 mike 1329: <dt>Contexts in which this element can be used:</dt>
1.1 mike 1330: <dd>In a <code><a href="semantics.html#the-head-element-0">head</a></code> element of an <a href="dom.html#html-documents" title="HTML documents">HTML document</a>, if there are no ancestor <code><a href="#the-noscript-element">noscript</a></code> elements.</dd>
1331: <dd>Where <a href="content-models.html#phrasing-content">phrasing content</a> is expected in <a href="dom.html#html-documents">HTML documents</a>, if there are no ancestor <code><a href="#the-noscript-element">noscript</a></code> elements.</dd>
1332: <dt>Content model:</dt>
1333: <dd>When <a href="webappapis.html#concept-n-noscript" title="concept-n-noscript">scripting is disabled</a>, in a <code><a href="semantics.html#the-head-element-0">head</a></code> element: in any order, zero or more <code><a href="semantics.html#the-link-element">link</a></code> elements, zero or more <code><a href="semantics.html#the-style-element">style</a></code> elements, and zero or more <code><a href="semantics.html#meta">meta</a></code> elements.</dd>
1334: <dd>When <a href="webappapis.html#concept-n-noscript" title="concept-n-noscript">scripting is disabled</a>, not in a <code><a href="semantics.html#the-head-element-0">head</a></code> element: <a href="content-models.html#transparent">transparent</a>, but there must be no <code><a href="#the-noscript-element">noscript</a></code> element descendants.</dd>
1335: <dd>Otherwise: text that conforms to the requirements given in the prose.</dd>
1336: <dt>Content attributes:</dt>
1337: <dd><a href="elements.html#global-attributes">Global attributes</a></dd>
1338: <dt>DOM interface:</dt>
1339: <dd>Uses <code><a href="elements.html#htmlelement">HTMLElement</a></code>.</dd>
1340: </dl><p>The <code><a href="#the-noscript-element">noscript</a></code> element <a href="rendering.html#represents">represents</a> nothing
1341: if <a href="webappapis.html#concept-n-script" title="concept-n-script">scripting is enabled</a>, and
1342: <a href="rendering.html#represents">represents</a> its children if <a href="webappapis.html#concept-n-noscript" title="concept-n-noscript">scripting is disabled</a>. It is used
1343: to present different markup to user agents that support scripting
1344: and those that don't support scripting, by affecting how the
1345: document is parsed.</p><p>When used in <a href="dom.html#html-documents">HTML documents</a>, the allowed content
1346: model is as follows:</p><dl><dt>In a <code><a href="semantics.html#the-head-element-0">head</a></code> element, if <a href="webappapis.html#concept-n-noscript" title="concept-n-noscript">scripting is disabled</a> for the
1347: <code><a href="#the-noscript-element">noscript</a></code> element</dt>
1348:
1349: <dd><p>The <code><a href="#the-noscript-element">noscript</a></code> element must contain only
1350: <code><a href="semantics.html#the-link-element">link</a></code>, <code><a href="semantics.html#the-style-element">style</a></code>, and <code><a href="semantics.html#meta">meta</a></code>
1351: elements.</p></dd>
1352:
1353: <dt>In a <code><a href="semantics.html#the-head-element-0">head</a></code> element, if <a href="webappapis.html#concept-n-script" title="concept-n-script">scripting is enabled</a> for the
1354: <code><a href="#the-noscript-element">noscript</a></code> element</dt>
1355:
1356: <dd><p>The <code><a href="#the-noscript-element">noscript</a></code> element must contain only text,
1357: except that invoking the <a href="the-end.html#html-fragment-parsing-algorithm">HTML fragment parsing
1358: algorithm</a> <!-- (which disables <script type="text/javascript"> execution) --> with
1359: the <code><a href="#the-noscript-element">noscript</a></code> element as the <var title="">context</var> element and the text contents as the <var title="">input</var> must result in a list of nodes that consists
1360: only of <code><a href="semantics.html#the-link-element">link</a></code>, <code><a href="semantics.html#the-style-element">style</a></code>, and
1361: <code><a href="semantics.html#meta">meta</a></code> elements that would be conforming if they were
1362: children of the <code><a href="#the-noscript-element">noscript</a></code> element, and no <a href="parsing.html#parse-error" title="parse error">parse errors</a>.</p></dd>
1363:
1364: <dt>Outside of <code><a href="semantics.html#the-head-element-0">head</a></code> elements, if <a href="webappapis.html#concept-n-noscript" title="concept-n-noscript">scripting is disabled</a> for the
1365: <code><a href="#the-noscript-element">noscript</a></code> element</dt>
1366:
1367: <dd><p>The <code><a href="#the-noscript-element">noscript</a></code> element's content model is
1368: <a href="content-models.html#transparent">transparent</a>, with the additional restriction that a
1369: <code><a href="#the-noscript-element">noscript</a></code> element must not have a <code><a href="#the-noscript-element">noscript</a></code>
1370: element as an ancestor (that is, <code><a href="#the-noscript-element">noscript</a></code> can't be
1371: nested).</p></dd>
1372:
1373: <dt>Outside of <code><a href="semantics.html#the-head-element-0">head</a></code> elements, if <a href="webappapis.html#concept-n-script" title="concept-n-script">scripting is enabled</a> for the
1374: <code><a href="#the-noscript-element">noscript</a></code> element</dt>
1375:
1376: <dd>
1377:
1378: <p>The <code><a href="#the-noscript-element">noscript</a></code> element must contain only text,
1379: except that the text must be such that running the following
1380: algorithm results in a conforming document with no
1381: <code><a href="#the-noscript-element">noscript</a></code> elements and no <code><a href="#script">script</a></code>
1382: elements, and such that no step in the algorithm causes an
1383: <a href="parsing.html#html-parser">HTML parser</a> to flag a <a href="parsing.html#parse-error">parse error</a>:</p>
1384:
1385: <ol><li>Remove every <code><a href="#script">script</a></code> element from the
1386: document.</li>
1387:
1388: <li>Make a list of every <code><a href="#the-noscript-element">noscript</a></code> element in the
1389: document. For every <code><a href="#the-noscript-element">noscript</a></code> element in that list,
1390: perform the following steps:
1391:
1392: <ol><li>Let the <var title="">parent element</var> be the parent
1393: element of the <code><a href="#the-noscript-element">noscript</a></code> element.</li>
1394:
1395: <li>Take all the children of the <var title="">parent element</var>
1396: that come before the <code><a href="#the-noscript-element">noscript</a></code> element, and call these
1397: elements <var title="">the before children</var>.</li>
1398:
1399: <li>Take all the children of the <var title="">parent element</var>
1400: that come <em>after</em> the <code><a href="#the-noscript-element">noscript</a></code> element, and
1401: call these elements <var title="">the after children</var>.</li>
1402:
1403: <li>Let <var title="">s</var> be the concatenation of all the
1404: <a href="infrastructure.html#text-node">text node</a> children of the <code><a href="#the-noscript-element">noscript</a></code>
1405: element.</li>
1406:
1407: <li>Set the <code title="dom-innerHTML"><a href="apis-in-html-documents.html#dom-innerhtml">innerHTML</a></code>
1408: attribute of the <var title="">parent element</var> to the value
1409: of <var title="">s</var>. (This, as a side-effect, causes the
1410: <code><a href="#the-noscript-element">noscript</a></code> element to be removed from the
1411: document.)</li>
1412:
1413: <li>Insert <var title="">the before children</var> at the start of
1414: the <var title="">parent element</var>, preserving their original
1415: relative order.</li>
1416:
1417: <li>Insert <var title="">the after children</var> at the end of the
1418: <var title="">parent element</var>, preserving their original
1419: relative order.</li>
1420:
1421: </ol></li>
1422:
1423: </ol></dd>
1424:
1425: </dl><p class="note">All these contortions are required because, for
1426: historical reasons, the <code><a href="#the-noscript-element">noscript</a></code> element is handled
1427: differently by the <a href="parsing.html#html-parser">HTML parser</a> based on whether <a href="parsing.html#scripting-flag" title="scripting flag">scripting was enabled or not</a> when the
1428: parser was invoked.</p><p>The <code><a href="#the-noscript-element">noscript</a></code> element must not be used in <a href="dom.html#xml-documents">XML
1429: documents</a>.</p><p class="note"><strong>The <code><a href="#the-noscript-element">noscript</a></code> element is only
1430: effective in <a href="syntax.html#syntax">the HTML syntax</a>, it has no effect in
1431: <a href="the-xhtml-syntax.html#the-xhtml-syntax">the XHTML syntax</a>.</strong></p><div class="impl">
1432:
1433: <p>The <code><a href="#the-noscript-element">noscript</a></code> element has no other requirements. In
1434: particular, children of the <code><a href="#the-noscript-element">noscript</a></code> element are not
1435: exempt from <a href="association-of-controls-and-forms.html#form-submission">form submission</a>, scripting, and so forth,
1436: even when <a href="webappapis.html#concept-n-script" title="concept-n-script">scripting is enabled</a>
1437: for the element.</p>
1438:
1439: </div><div class="example">
1440:
1441: <p>In the following example, a <code><a href="#the-noscript-element">noscript</a></code> element is
1442: used to provide fallback for a script.</p>
1443:
1444: <pre><form action="calcSquare.php">
1445: <p>
1446: <label for=x>Number</label>:
1447: <input id="x" name="x" type="number">
1448: </p>
1449: <script>
1450: var x = document.getElementById('x');
1451: var output = document.createElement('p');
1452: output.textContent = 'Type a number; it will be squared right then!';
1453: x.form.appendChild(output);
1454: x.form.onsubmit = function () { return false; }
1455: x.oninput = function () {
1456: var v = x.valueAsNumber;
1457: output.textContent = v + ' squared is ' + v * v;
1458: };
1459: </script>
1460: <noscript>
1461: <input type=submit value="Calculate Square">
1462: </noscript>
1463: </form></pre>
1464:
1465: <p>When script is disabled, a button appears to do the calculation
1466: on the server side. When script is enabled, the value is computed
1467: on-the-fly instead.</p>
1468:
1469: <p>The <code><a href="#the-noscript-element">noscript</a></code> element is a blunt
1470: instrument. Sometimes, scripts might be enabled, but for some
1471: reason the page's script might fail. For this reason, it's
1472: generally better to avoid using <code><a href="#the-noscript-element">noscript</a></code>, and to
1473: instead design the script to change the page from being a
1474: scriptless page to a scripted page on the fly, as in the next
1475: example:</p>
1476:
1477: <pre><form action="calcSquare.php">
1478: <p>
1479: <label for=x>Number</label>:
1480: <input id="x" name="x" type="number">
1481: </p>
1482: <strong><input id="submit" type=submit value="Calculate Square"></strong>
1483: <script>
1484: var x = document.getElementById('x');
1485: var output = document.createElement('p');
1486: output.textContent = 'Type a number; it will be squared right then!';
1487: x.form.appendChild(output);
1488: x.form.onsubmit = function () { return false; }
1489: x.oninput = function () {
1490: var v = x.valueAsNumber;
1491: output.textContent = v + ' squared is ' + v * v;
1492: };
1493: <strong> var submit = document.getElementById('submit');
1494: submit.parentNode.removeChild(submit);</strong>
1495: </script>
1496: </form></pre>
1497:
1498: <p>The above technique is also useful in XHTML, since
1499: <code><a href="#the-noscript-element">noscript</a></code> is not supported in <a href="the-xhtml-syntax.html#the-xhtml-syntax">the XHTML
1500: syntax</a>.</p>
1501:
1502: </div></body></html>
Webmaster