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