Annotation of html5/spec-author-view/the-script-element.html, revision 1.130
1.104 mike 1: <!doctype html>
1.122 mike 2: <html lang="en-US-x-Hixie"><meta content="text/html; charset=utf-8" http-equiv="Content-Type"><title>4.3.1 The script element — HTML5: Edition for Web Authors</title><link href="style.css" rel="stylesheet"><link href="http://www.w3.org/StyleSheets/TR/W3C-ED" rel="stylesheet" type="text/css"><script src="link-fixup.js" type="text/javascript"></script><style type="text/css">
1.1 mike 3:
4: .applies thead th > * { display: block; }
5: .applies thead code { display: block; }
6: .applies tbody th { whitespace: nowrap; }
7: .applies td { text-align: center; }
8: .applies .yes { background: yellow; }
9:
10: .matrix, .matrix td { border: hidden; text-align: right; }
11: .matrix { margin-left: 2em; }
12:
13: .dice-example { border-collapse: collapse; border-style: hidden solid solid hidden; border-width: thin; margin-left: 3em; }
14: .dice-example caption { width: 30em; font-size: smaller; font-style: italic; padding: 0.75em 0; text-align: left; }
15: .dice-example td, .dice-example th { border: solid thin; width: 1.35em; height: 1.05em; text-align: center; padding: 0; }
16:
17: td.eg { border-width: thin; text-align: center; }
18:
19: #table-example-1 { border: solid thin; border-collapse: collapse; margin-left: 3em; }
20: #table-example-1 * { font-family: "Essays1743", serif; line-height: 1.01em; }
21: #table-example-1 caption { padding-bottom: 0.5em; }
22: #table-example-1 thead, #table-example-1 tbody { border: none; }
23: #table-example-1 th, #table-example-1 td { border: solid thin; }
24: #table-example-1 th { font-weight: normal; }
25: #table-example-1 td { border-style: none solid; vertical-align: top; }
26: #table-example-1 th { padding: 0.5em; vertical-align: middle; text-align: center; }
27: #table-example-1 tbody tr:first-child td { padding-top: 0.5em; }
28: #table-example-1 tbody tr:last-child td { padding-bottom: 1.5em; }
29: #table-example-1 tbody td:first-child { padding-left: 2.5em; padding-right: 0; width: 9em; }
30: #table-example-1 tbody td:first-child::after { content: leader(". "); }
31: #table-example-1 tbody td { padding-left: 2em; padding-right: 2em; }
32: #table-example-1 tbody td:first-child + td { width: 10em; }
33: #table-example-1 tbody td:first-child + td ~ td { width: 2.5em; }
34: #table-example-1 tbody td:first-child + td + td + td ~ td { width: 1.25em; }
35:
36: .apple-table-examples { border: none; border-collapse: separate; border-spacing: 1.5em 0em; width: 40em; margin-left: 3em; }
37: .apple-table-examples * { font-family: "Times", serif; }
38: .apple-table-examples td, .apple-table-examples th { border: none; white-space: nowrap; padding-top: 0; padding-bottom: 0; }
39: .apple-table-examples tbody th:first-child { border-left: none; width: 100%; }
40: .apple-table-examples thead th:first-child ~ th { font-size: smaller; font-weight: bolder; border-bottom: solid 2px; text-align: center; }
41: .apple-table-examples tbody th::after, .apple-table-examples tfoot th::after { content: leader(". ") }
42: .apple-table-examples tbody th, .apple-table-examples tfoot th { font: inherit; text-align: left; }
43: .apple-table-examples td { text-align: right; vertical-align: top; }
44: .apple-table-examples.e1 tbody tr:last-child td { border-bottom: solid 1px; }
45: .apple-table-examples.e1 tbody + tbody tr:last-child td { border-bottom: double 3px; }
46: .apple-table-examples.e2 th[scope=row] { padding-left: 1em; }
47: .apple-table-examples sup { line-height: 0; }
48:
49: .details-example img { vertical-align: top; }
50:
51: #base64-table {
52: white-space: nowrap;
53: font-size: 0.6em;
54: column-width: 6em;
55: column-count: 5;
56: column-gap: 1em;
57: -moz-column-width: 6em;
58: -moz-column-count: 5;
59: -moz-column-gap: 1em;
60: -webkit-column-width: 6em;
61: -webkit-column-count: 5;
62: -webkit-column-gap: 1em;
63: }
64: #base64-table thead { display: none; }
65: #base64-table * { border: none; }
66: #base64-table tbody td:first-child:after { content: ':'; }
67: #base64-table tbody td:last-child { text-align: right; }
68:
69: #named-character-references-table {
70: white-space: nowrap;
71: font-size: 0.6em;
72: column-width: 30em;
73: column-gap: 1em;
74: -moz-column-width: 30em;
75: -moz-column-gap: 1em;
76: -webkit-column-width: 30em;
77: -webkit-column-gap: 1em;
78: }
79: #named-character-references-table > table > tbody > tr > td:first-child + td,
80: #named-character-references-table > table > tbody > tr > td:last-child { text-align: center; }
81: #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; }
82: #named-character-references-table > table > tbody > tr#entity-CounterClockwiseContourIntegral > td:first-child { font-size: 0.5em; }
83:
84: .glyph.control { color: red; }
85:
86: @font-face {
87: font-family: 'Essays1743';
88: src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743.ttf');
89: }
90: @font-face {
91: font-family: 'Essays1743';
92: font-weight: bold;
93: src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743-Bold.ttf');
94: }
95: @font-face {
96: font-family: 'Essays1743';
97: font-style: italic;
98: src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743-Italic.ttf');
99: }
100: @font-face {
101: font-family: 'Essays1743';
102: font-style: italic;
103: font-weight: bold;
104: src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743-BoldItalic.ttf');
105: }
106:
107: </style><script type="text/javascript">
108: function getCookie(name) {
109: var params = location.search.substr(1).split("&");
110: for (var index = 0; index < params.length; index++) {
111: if (params[index] == name)
112: return "1";
113: var data = params[index].split("=");
114: if (data[0] == name)
115: return unescape(data[1]);
116: }
117: var cookies = document.cookie.split("; ");
118: for (var index = 0; index < cookies.length; index++) {
119: var data = cookies[index].split("=");
120: if (data[0] == name)
121: return unescape(data[1]);
122: }
123: return null;
124: }
125: function load(script) {
126: var e = document.createElement('script');
127: e.setAttribute('src', script + '?' + encodeURIComponent(location) + '&' + encodeURIComponent(document.referrer));
128: document.body.appendChild(e);
129: }
130: function init() {
131: if (location.search == '?slow-browser')
132: return;
133: var configUI = document.createElement('div');
134: configUI.id = 'configUI';
135: document.body.appendChild(configUI);
136: if (document.getElementById('head'))
137: load('toc.js');
138: load('dfn.js');
139: if (getCookie('profile') == '1')
140: document.getElementsByTagName('h2')[0].textContent += '; load: ' + (new Date() - loadTimer) + 'ms';
141: fixBrokenLink();
142: }
1.105 mike 143: </script><script src="dfn.js" type="text/javascript"></script><script src="toc.js" type="text/javascript"></script><link href="scripting-1.html" rel="prev" title="4.3 Scripting">
144: <link href="Overview.html#contents" rel="contents" title="Table of contents">
1.97 mike 145: <link href="the-noscript-element.html" rel="next" title="4.3.2 The noscript element">
1.105 mike 146: <body class="split chapter"><div class="head" id="head">
1.9 mike 147: <div aria-haspopup="true" id="jumpIndexA-button" role="button" tabindex="0">jump
1.128 mike 148: <div id="jumpIndexA"><ul><li><a class="noindex" href="the-a-element.html#the-a-element">a</a><a class="noindex" href="the-abbr-element.html#the-abbr-element">abbr</a><a class="noindex" href="the-address-element.html#the-address-element">address</a><a class="noindex" href="the-area-element.html#the-area-element">area</a><a class="noindex" href="the-article-element.html#the-article-element">article</a><a class="noindex" href="the-aside-element.html#the-aside-element">aside</a><a class="noindex" href="the-audio-element.html#the-audio-element">audio</a></ul><ul><li><a class="noindex" href="the-b-element.html#the-b-element">b</a><a class="noindex" href="the-base-element.html#the-base-element">base</a><a class="noindex" href="the-bdi-element.html#the-bdi-element">bdi</a><a class="noindex" href="the-bdo-element.html#the-bdo-element">bdo</a><a class="noindex" href="the-blockquote-element.html#the-blockquote-element">blockquote</a><a class="noindex" href="the-body-element.html#the-body-element">body</a><a class="noindex" href="the-br-element.html#the-br-element">br</a><a class="noindex" href="the-button-element.html#the-button-element">button</a></ul><ul><li><a class="noindex" href="the-canvas-element.html#the-canvas-element">canvas</a><a class="noindex" href="the-caption-element.html#the-caption-element">caption</a><a class="noindex" href="the-cite-element.html#the-cite-element">cite</a><a class="noindex" href="the-code-element.html#the-code-element">code</a><a class="noindex" href="the-col-element.html#the-col-element">col</a><a class="noindex" href="the-colgroup-element.html#the-colgroup-element">colgroup</a><a class="noindex" href="the-command-element.html#the-command-element">command</a></ul><ul><li><a class="noindex" href="the-datalist-element.html#the-datalist-element">datalist</a><a class="noindex" href="the-dd-element.html#the-dd-element">dd</a><a class="noindex" href="the-del-element.html#the-del-element">del</a><a class="noindex" href="the-details-element.html#the-details-element">details</a><a class="noindex" href="the-dfn-element.html#the-dfn-element">dfn</a><a class="noindex" href="the-div-element.html#the-div-element">div</a><a class="noindex" href="the-dl-element.html#the-dl-element">dl</a><a class="noindex" href="the-dt-element.html#the-dt-element">dt</a></ul><ul><li><a class="noindex" href="the-em-element.html#the-em-element">em</a><a class="noindex" href="the-embed-element.html#the-embed-element">embed</a><a class="noindex" href="the-fieldset-element.html#the-fieldset-element">fieldset</a><a class="noindex" href="the-figcaption-element.html#the-figcaption-element">figcaption</a><a class="noindex" href="the-figure-element.html#the-figure-element">figure</a><a class="noindex" href="the-footer-element.html#the-footer-element">footer</a><a class="noindex" href="the-form-element.html#the-form-element">form</a></ul><ul><li><a class="noindex" href="the-h1-h2-h3-h4-h5-and-h6-elements.html#the-h1-h2-h3-h4-h5-and-h6-elements">h1</a><a class="noindex" href="the-h1-h2-h3-h4-h5-and-h6-elements.html#the-h1-h2-h3-h4-h5-and-h6-elements">h2</a><a class="noindex" href="the-h1-h2-h3-h4-h5-and-h6-elements.html#the-h1-h2-h3-h4-h5-and-h6-elements">h3</a><a class="noindex" href="the-h1-h2-h3-h4-h5-and-h6-elements.html#the-h1-h2-h3-h4-h5-and-h6-elements">h4</a><a class="noindex" href="the-h1-h2-h3-h4-h5-and-h6-elements.html#the-h1-h2-h3-h4-h5-and-h6-elements">h5</a><a class="noindex" href="the-h1-h2-h3-h4-h5-and-h6-elements.html#the-h1-h2-h3-h4-h5-and-h6-elements">h6</a><a class="noindex" href="the-head-element.html#the-head-element">head</a><a class="noindex" href="the-header-element.html#the-header-element">header</a><a class="noindex" href="the-hgroup-element.html#the-hgroup-element">hgroup</a><a class="noindex" href="the-hr-element.html#the-hr-element">hr</a><a class="noindex" href="the-html-element.html#the-html-element">html</a></ul><ul><li><a class="noindex" href="the-i-element.html#the-i-element">i</a><a class="noindex" href="the-iframe-element.html#the-iframe-element">iframe</a><a class="noindex" href="the-img-element.html#the-img-element">img</a><a class="noindex" href="the-input-element.html#the-input-element">input</a><a class="noindex" href="the-ins-element.html#the-ins-element">ins</a><a class="noindex" href="the-kbd-element.html#the-kbd-element">kbd</a><a class="noindex" href="the-keygen-element.html#the-keygen-element">keygen</a></ul><ul><li><a class="noindex" href="the-label-element.html#the-label-element">label</a><a class="noindex" href="the-legend-element.html#the-legend-element">legend</a><a class="noindex" href="the-li-element.html#the-li-element">li</a><a class="noindex" href="the-link-element.html#the-link-element">link</a><a class="noindex" href="the-map-element.html#the-map-element">map</a><a class="noindex" href="the-mark-element.html#the-mark-element">mark</a><a class="noindex" href="the-menu-element.html#the-menu-element">menu</a><a class="noindex" href="the-meta-element.html#the-meta-element">meta</a><a class="noindex" href="the-meter-element.html#the-meter-element">meter</a></ul><ul><li><a class="noindex" href="the-nav-element.html#the-nav-element">nav</a><a class="noindex" href="the-noscript-element.html#the-noscript-element">noscript</a><a class="noindex" href="the-object-element.html#the-object-element">object</a><a class="noindex" href="the-ol-element.html#the-ol-element">ol</a><a class="noindex" href="the-optgroup-element.html#the-optgroup-element">optgroup</a><a class="noindex" href="the-option-element.html#the-option-element">option</a><a class="noindex" href="the-output-element.html#the-output-element">output</a></ul><ul><li><a class="noindex" href="the-p-element.html#the-p-element">p</a><a class="noindex" href="the-param-element.html#the-param-element">param</a><a class="noindex" href="the-pre-element.html#the-pre-element">pre</a><a class="noindex" href="the-progress-element.html#the-progress-element">progress</a><a class="noindex" href="the-q-element.html#the-q-element">q</a><a class="noindex" href="the-rp-element.html#the-rp-element">rp</a><a class="noindex" href="the-rt-element.html#the-rt-element">rt</a><a class="noindex" href="the-ruby-element.html#the-ruby-element">ruby</a></ul><ul><li><a class="noindex" href="the-s-element.html#the-s-element">s</a><a class="noindex" href="the-samp-element.html#the-samp-element">samp</a><a class="noindex" href="#the-script-element">script</a><a class="noindex" href="the-section-element.html#the-section-element">section</a><a class="noindex" href="the-select-element.html#the-select-element">select</a><a class="noindex" href="the-small-element.html#the-small-element">small</a><a class="noindex" href="the-source-element.html#the-source-element">source</a><a class="noindex" href="the-span-element.html#the-span-element">span</a></ul><ul><li><a class="noindex" href="the-strong-element.html#the-strong-element">strong</a><a class="noindex" href="the-style-element.html#the-style-element">style</a><a class="noindex" href="the-sub-and-sup-elements.html#the-sub-and-sup-elements">sub</a><a class="noindex" href="the-summary-element.html#the-summary-element">summary</a><a class="noindex" href="the-sub-and-sup-elements.html#the-sub-and-sup-elements">sup</a></ul><ul><li><a class="noindex" href="the-table-element.html#the-table-element">table</a><a class="noindex" href="the-tbody-element.html#the-tbody-element">tbody</a><a class="noindex" href="the-td-element.html#the-td-element">td</a><a class="noindex" href="the-textarea-element.html#the-textarea-element">textarea</a><a class="noindex" href="the-tfoot-element.html#the-tfoot-element">tfoot</a><a class="noindex" href="the-th-element.html#the-th-element">th</a><a class="noindex" href="the-thead-element.html#the-thead-element">thead</a><a class="noindex" href="the-time-element.html#the-time-element">time</a><a class="noindex" href="the-title-element.html#the-title-element">title</a></ul><ul><li><a class="noindex" href="the-tr-element.html#the-tr-element">tr</a><a class="noindex" href="the-track-element.html#the-track-element">track</a><a class="noindex" href="the-u-element.html#the-u-element">u</a><a class="noindex" href="the-ul-element.html#the-ul-element">ul</a><a class="noindex" href="the-var-element.html#the-var-element">var</a><a class="noindex" href="the-video-element.html#the-video-element">video</a><a class="noindex" href="the-wbr-element.html#the-wbr-element">wbr</a></ul><ul class="jumpIndexA-other"><li><a class="noindex" href="global-attributes.html#global-attributes">global attributes</a><a class="noindex" href="index-of-terms.html#index-of-terms">terms</a></ul></div></div>
1.11 mike 149: <script src="jump-indexes.js"></script><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.1 mike 150:
1.97 mike 151: <h1>HTML5: Edition for Web Authors</h1>
152:
153: </div><div class="prev_next">
154: <a href="scripting-1.html">← 4.3 Scripting</a> –
1.1 mike 155: <a href="Overview.html#contents">Table of contents</a> –
1.97 mike 156: <a href="the-noscript-element.html">4.3.2 The noscript element →</a>
1.102 mike 157: <ol class="toc"><li><ol><li><ol><li><a href="the-script-element.html#the-script-element" id="4.3.1-the-script-element"><span class="secno">4.3.1 </span>The <code>script</code> element</a>
158: <ol><li><a href="the-script-element.html#scriptingLanguages"><span class="secno">4.3.1.1 </span>Scripting languages</a><li><a href="the-script-element.html#restrictions-for-contents-of-script-elements" id="4.3.1.2-restrictions-for-contents-of-script-elements"><span class="secno">4.3.1.2 </span>Restrictions for contents of <code>script</code> elements</a><li><a href="the-script-element.html#inline-documentation-for-external-scripts" id="4.3.1.3-inline-documentation-for-external-scripts"><span class="secno">4.3.1.3 </span>Inline documentation for external scripts</a></ol></ol></ol></ol></div>
1.1 mike 159:
1.97 mike 160: <h4 id="the-script-element"><span class="secno">4.3.1 </span>The <dfn aria-haspopup="true" role="button" tabindex="0"><code>script</code></dfn> element</h4><dl class="element"><dt>Categories</dt>
1.1 mike 161: <dd><a href="content-models.html#metadata-content" id="metadata-content_12">Metadata content</a>.</dd>
1.123 mike 162: <dd><a href="content-models.html#flow-content" id="flow-content_14">Flow content</a>.</dd>
163: <dd><a href="content-models.html#phrasing-content" id="phrasing-content_20">Phrasing content</a>.</dd>
1.1 mike 164: <dt>Contexts in which this element can be used:</dt>
165: <dd>Where <a href="content-models.html#metadata-content" id="metadata-content_13">metadata content</a> is expected.</dd>
1.123 mike 166: <dd>Where <a href="content-models.html#phrasing-content" id="phrasing-content_21">phrasing content</a> is expected.</dd>
1.1 mike 167: <dt>Content model:</dt>
168: <dd>If there is no <code title="attr-script-src"><a href="#attr-script-src" id="src_1">src</a></code>
1.114 mike 169: attribute, depends on the value of the <code title="attr-script-type"><a href="#attr-script-type" id="type_6">type</a></code> attribute, but must match
1.1 mike 170: <a href="#restrictions-for-contents-of-script-elements" id="script-content-restrictions">script content restrictions</a>.</dd>
171: <dd>If there <em>is</em> a <code title="attr-script-src"><a href="#attr-script-src" id="src_2">src</a></code>
172: attribute, the element must be either empty or contain only
173: <a href="#inline-documentation-for-external-scripts" id="script-documentation">script documentation</a> that also matches <a href="#restrictions-for-contents-of-script-elements" id="script-content-restrictions_0">script
174: content restrictions</a>.</dd>
175: <dt>Content attributes:</dt>
1.6 mike 176: <dd><a href="global-attributes.html#global-attributes" id="global-attributes_7">Global attributes</a></dd>
1.1 mike 177: <dd><code title="attr-script-src"><a href="#attr-script-src" id="src_3">src</a></code></dd>
178: <dd><code title="attr-script-async"><a href="#attr-script-async" id="async">async</a></code></dd>
179: <dd><code title="attr-script-defer"><a href="#attr-script-defer" id="defer">defer</a></code></dd>
1.114 mike 180: <dd><code title="attr-script-type"><a href="#attr-script-type" id="type_7">type</a></code></dd>
181: <dd><code title="attr-script-charset"><a href="#attr-script-charset" id="charset_10">charset</a></code></dd>
1.1 mike 182: <dt>DOM interface:</dt>
183: <dd>
1.109 mike 184: <pre class="idl">interface <dfn aria-haspopup="true" id="htmlscriptelement" role="button" tabindex="0">HTMLScriptElement</dfn> : <a href="elements.html#htmlelement" id="htmlelement_13">HTMLElement</a> {
1.92 mike 185: attribute DOMString <a href="#dom-script-src" id="dom-script-src_0" title="dom-script-src">src</a>;
186: attribute boolean <a href="#dom-script-async" id="dom-script-async_0" title="dom-script-async">async</a>;
187: attribute boolean <a href="#dom-script-defer" id="dom-script-defer_0" title="dom-script-defer">defer</a>;
188: attribute DOMString <a href="#dom-script-type" id="dom-script-type_0" title="dom-script-type">type</a>;
189: attribute DOMString <a href="#dom-script-charset" id="dom-script-charset_0" title="dom-script-charset">charset</a>;
1.91 mike 190: attribute DOMString <a href="#dom-script-text" title="dom-script-text">text</a>;
1.1 mike 191: };</pre>
192: </dd>
1.130 ! mike 193: </dl><p>The <code><a href="#the-script-element" id="script_14">script</a></code> element allows authors to include dynamic
1.1 mike 194: script and data blocks in their documents. The element does not
1.114 mike 195: <a class="full-spec-link" href="http://dev.w3.org/html5/spec/rendering.html#represents" title='Read about this "represent" reference in the full HTML5 spec.'>represent</a> content for the user.<p>When used to include dynamic scripts, the scripts may either be
1.1 mike 196: embedded inline or may be imported from an external file using the
197: <code title="attr-script-src"><a href="#attr-script-src" id="src_4">src</a></code> attribute. If the language
198: is not that described by "<code title="">text/javascript</code>",
1.114 mike 199: then the <code title="attr-script-type"><a href="#attr-script-type" id="type_8">type</a></code> attribute must
1.1 mike 200: be present, as described below. Whatever language is used, the
1.130 ! mike 201: contents of the <code><a href="#the-script-element" id="script_15">script</a></code> element must conform with the
1.1 mike 202: requirements of that language's specification.<p>When used to include data blocks (as opposed to scripts), the
203: data must be embedded inline, the format of the data must be given
1.114 mike 204: using the <code title="attr-script-type"><a href="#attr-script-type" id="type_9">type</a></code> attribute, the
1.1 mike 205: <code title="attr-script-src"><a href="#attr-script-src" id="src_5">src</a></code> attribute must not be
1.130 ! mike 206: specified, and the contents of the <code><a href="#the-script-element" id="script_16">script</a></code> element must
1.1 mike 207: conform to the requirements defined for the format used.<p>The <dfn aria-haspopup="true" id="attr-script-type" role="button" tabindex="0"><code>type</code></dfn>
208: attribute gives the language of the script or format of the data. If
209: the attribute is present, its value must be a <a href="infrastructure.html#valid-mime-type" id="valid-mime-type_3">valid MIME
210: type</a>. The <code title="">charset</code> parameter must not be
211: specified. The default, which is used if the attribute is absent,
212: is "<code title="">text/javascript</code>".<p>The <dfn aria-haspopup="true" id="attr-script-src" role="button" tabindex="0"><code>src</code></dfn>
213: attribute, if specified, gives the address of the external script
214: resource to use. The value of the attribute must be a <a href="urls.html#valid-non-empty-url-potentially-surrounded-by-spaces" id="valid-non-empty-url-potentially-surrounded-by-spaces_2">valid
215: non-empty URL potentially surrounded by spaces</a> identifying a
1.114 mike 216: script resource of the type given by the <code title="attr-script-type"><a href="#attr-script-type" id="type_10">type</a></code> attribute, if the attribute is
1.1 mike 217: present, or of the type "<code title="">text/javascript</code>", if
218: the attribute is absent. A resource is a script resource of a given
219: type if that type identifies a scripting language and the resource
220: conforms with the requirements of that language's specification.<p>The <dfn aria-haspopup="true" id="attr-script-charset" role="button" tabindex="0"><code>charset</code></dfn>
221: attribute gives the character encoding of the external script
222: resource. The attribute must not be specified if the <code title="attr-script-src"><a href="#attr-script-src" id="src_6">src</a></code> attribute is not present. If the
223: attribute is set, its value must be a valid character encoding name,
224: must be an <a href="infrastructure.html#ascii-case-insensitive" id="ascii-case-insensitive_10">ASCII case-insensitive</a> match for the
225: <a href="infrastructure.html#preferred-mime-name" id="preferred-mime-name_1">preferred MIME name</a> for that encoding, and must match
226: the encoding given in the <code title="">charset</code> parameter of
1.129 mike 227: the <a class="full-spec-link" href="http://dev.w3.org/html5/spec/urls.html#content-type" title='Read about this "Content-Type metadata" reference in the full HTML5 spec.'>Content-Type metadata</a> of the
1.114 mike 228: external file, if any. <a href="references.html#refsIANACHARSET">[IANACHARSET]</a><p>The <dfn aria-haspopup="true" id="attr-script-async" role="button" tabindex="0"><code>async</code></dfn> and
1.1 mike 229: <dfn aria-haspopup="true" id="attr-script-defer" role="button" tabindex="0"><code>defer</code></dfn> attributes
230: are <a href="common-microsyntaxes.html#boolean-attribute" id="boolean-attribute_1" title="boolean attribute">boolean attributes</a> that
231: indicate how the script should be executed. The <code title="attr-script-defer"><a href="#attr-script-defer" id="defer_0">defer</a></code> and <code title="attr-script-async"><a href="#attr-script-async" id="async_0">async</a></code> attributes must not be
232: specified if the <code title="attr-script-src"><a href="#attr-script-src" id="src_7">src</a></code> attribute
233: is not present.<p>There are three possible modes that can be selected using these
234: attributes. If the <code title="attr-script-async"><a href="#attr-script-async" id="async_1">async</a></code>
235: attribute is present, then the script will be executed
236: asynchronously, as soon as it is available. If the <code title="attr-script-async"><a href="#attr-script-async" id="async_2">async</a></code> attribute is not present but
237: the <code title="attr-script-defer"><a href="#attr-script-defer" id="defer_1">defer</a></code> attribute is
238: present, then the script is executed when the page has finished
239: parsing. If neither attribute is present, then the script is
240: fetched and executed immediately, before the user agent continues
241: parsing the page.<p class="note">The exact processing details for these attributes
242: are, for mostly historical reasons, somewhat non-trivial, involving
243: a number of aspects of HTML. The implementation requirements are
244: therefore by necessity scattered throughout the specification. The
245: algorithms below (in this section) describe the core of this
246: processing, but these algorithms reference and are referenced by the
1.130 ! mike 247: parsing rules for <code><a href="#the-script-element" id="script_17">script</a></code> <a class="full-spec-link" href="http://dev.w3.org/html5/spec/tree-construction.html#scriptTag" title='Read about this "start" reference in the full HTML5 spec.'>start</a>
1.114 mike 248: and <a class="full-spec-link" href="http://dev.w3.org/html5/spec/tree-construction.html#scriptEndTag" title='Read about this "end" reference in the full HTML5 spec.'>end</a> tags in HTML, <a class="full-spec-link" href="http://dev.w3.org/html5/spec/tree-construction.html#scriptForeignEndTag" title='Read about this "in foreign content" reference in the full HTML5 spec.'>in foreign content</a>, and <a class="full-spec-link" href="http://dev.w3.org/html5/spec/the-xhtml-syntax.html#scriptTagXML" title='Read about this "in XML" reference in the full HTML5 spec.'>in XML</a>, the rules for the <code title="dom-document-write"><a href="dynamic-markup-insertion.html#dom-document-write">document.write()</a></code> method, the
1.1 mike 249: handling of <a href="webappapis.html#scripting">scripting</a>, etc.<p>The <code title="attr-script-defer"><a href="#attr-script-defer" id="defer_2">defer</a></code> attribute may be
250: specified even if the <code title="attr-script-async"><a href="#attr-script-async" id="async_3">async</a></code>
251: attribute is specified, to cause legacy Web browsers that only
252: support <code title="attr-script-defer"><a href="#attr-script-defer" id="defer_3">defer</a></code> (and not <code title="attr-script-async"><a href="#attr-script-async" id="async_4">async</a></code>) to fall back to the <code title="attr-script-defer"><a href="#attr-script-defer" id="defer_4">defer</a></code> behavior instead of the
1.114 mike 253: synchronous blocking behavior that is the default.<p>Changing the <code title="attr-script-src"><a href="#attr-script-src" id="src_8">src</a></code>, <code title="attr-script-type"><a href="#attr-script-type" id="type_11">type</a></code>, <code title="attr-script-charset"><a href="#attr-script-charset" id="charset_11">charset</a></code>, <code title="attr-script-async"><a href="#attr-script-async" id="async_5">async</a></code>, and <code title="attr-script-defer"><a href="#attr-script-defer" id="defer_5">defer</a></code> attributes dynamically has no
1.1 mike 254: direct effect; these attribute are only used at specific times
1.102 mike 255: described below.</p>
256: <p>The IDL attributes <dfn aria-haspopup="true" id="dom-script-src" role="button" tabindex="0"><code>src</code></dfn>, <dfn aria-haspopup="true" id="dom-script-type" role="button" tabindex="0"><code>type</code></dfn>, <dfn aria-haspopup="true" id="dom-script-charset" role="button" tabindex="0"><code>charset</code></dfn>, and <dfn aria-haspopup="true" id="dom-script-defer" role="button" tabindex="0"><code>defer</code></dfn>, each must
1.120 mike 257: <a href="common-dom-interfaces.html#reflect" id="reflect_12">reflect</a> the respective content attributes of the same
1.102 mike 258: name.</p>
259:
260: <p>The <dfn aria-haspopup="true" id="dom-script-async" role="button" tabindex="0"><code>async</code></dfn> IDL
1.92 mike 261: attribute controls whether the element will execute asynchronously
1.129 mike 262: or not. If the element's <a class="full-spec-link" href="http://dev.w3.org/html5/spec/the-script-element.html#force-async" title='Read about this ""force-async"" reference in the full HTML5 spec.'>"force-async"</a> flag is set,
1.114 mike 263: then, on getting, the <code title="dom-script-async"><a href="#dom-script-async" id="async_8">async</a></code>
1.92 mike 264: IDL attribute must return true, and on setting, the
1.129 mike 265: <a class="full-spec-link" href="http://dev.w3.org/html5/spec/the-script-element.html#force-async" title='Read about this ""force-async"" reference in the full HTML5 spec.'>"force-async"</a> flag must first be unset, and then the
1.92 mike 266: content attribute must be removed if the IDL attribute's new value
267: is false, and must be set to the empty string if the IDL attribute's
1.129 mike 268: new value is true. If the element's <a class="full-spec-link" href="http://dev.w3.org/html5/spec/the-script-element.html#force-async" title='Read about this ""force-async"" reference in the full HTML5 spec.'>"force-async"</a> flag
1.120 mike 269: is <em>not</em> set, the IDL attribute must <a href="common-dom-interfaces.html#reflect" id="reflect_13">reflect</a> the
1.102 mike 270: <code title="attr-script-async"><a href="#attr-script-async" id="async_6">async</a></code> content attribute.</p>
271: <dl class="domintro"><dt><var title="">script</var> . <code id="dom-script-text" title="dom-script-text"><a href="#dom-script-text">text</a></code> [ = <var title="">value</var> ]</dt>
1.1 mike 272:
273: <dd>
274:
275: <p>Returns the contents of the element, ignoring child nodes that
1.123 mike 276: aren't <a href="infrastructure.html#text-node" id="text-node_10" title="text node">text nodes</a>.</p>
1.1 mike 277:
278: <p>Can be set, to replace the element's children with the given
279: value.</p>
280:
281: </dd>
282:
1.105 mike 283: </dl><p class="note">When inserted using the <code title="dom-document-write"><a href="dynamic-markup-insertion.html#dom-document-write">document.write()</a></code> method,
1.130 ! mike 284: <code><a href="#the-script-element" id="script_18">script</a></code> elements execute (typically synchronously), but
1.125 mike 285: when inserted using <code title="dom-innerHTML"><a class="full-spec-link" href="http://dev.w3.org/html5/spec/infrastructure.html#dom-innerhtml" title='Read about this "innerHTML" reference in the full HTML5 spec.'>innerHTML</a></code> and <code title="dom-outerHTML"><a class="full-spec-link" href="http://dev.w3.org/html5/spec/infrastructure.html#dom-outerhtml" title='Read about this "outerHTML" reference in the full HTML5 spec.'>outerHTML</a></code> attributes, they do not
1.1 mike 286: execute at all.<div class="example">
287:
1.130 ! mike 288: <p>In this example, two <code><a href="#the-script-element" id="script_19">script</a></code> elements are used. One
1.1 mike 289: embeds an external script, and the other includes some data.</p>
290:
291: <pre><script src="game-engine.js"></script>
292: <script type="text/x-game-map">
293: ........U.........e
294: o............A....e
295: .....A.....AAA....e
296: .A..AAA...AAAAA...e
297: </script></pre>
298:
299: <p>The data in this case might be used by the script to generate
300: the map of a video game. The data doesn't have to be used that way,
301: though; maybe the map data is actually embedded in other parts of
302: the page's markup, and the data block here is just used by the
303: site's search engine to help users who are looking for particular
304: features in their game maps.</p>
305:
306: </div><div class="example">
307:
308: <p>The following sample shows how a script element can be used to
309: define a function that is then used by other parts of the
1.130 ! mike 310: document. It also shows how a <code><a href="#the-script-element" id="script_20">script</a></code> element can be
1.1 mike 311: used to invoke script while the document is being parsed, in this
312: case to initialize the form's output.</p>
313:
314: <pre><script>
315: function calculate(form) {
316: var price = 52000;
317: if (form.elements.brakes.checked)
318: price += 1000;
319: if (form.elements.radio.checked)
320: price += 2500;
321: if (form.elements.turbo.checked)
322: price += 5000;
323: if (form.elements.sticker.checked)
324: price += 250;
325: form.elements.result.value = price;
326: }
327: </script>
328: <form name="pricecalc" onsubmit="return false" onchange="calculate(this)">
329: <fieldset>
330: <legend>Work out the price of your car</legend>
331: <p>Base cost: £52000.</p>
332: <p>Select additional options:</p>
333: <ul>
334: <li><label><input type=checkbox name=brakes> Ceramic brakes (£1000)</label></li>
335: <li><label><input type=checkbox name=radio> Satellite radio (£2500)</label></li>
336: <li><label><input type=checkbox name=turbo> Turbo charger (£5000)</label></li>
337: <li><label><input type=checkbox name=sticker> "XZ" sticker (£250)</label></li>
338: </ul>
339: <p>Total: £<output name=result></output></p>
340: </fieldset>
341: <script>
342: calculate(document.forms.pricecalc);
343: </script>
344: </form></pre>
345:
1.126 mike 346: </div><h5 id="scriptingLanguages"><span class="secno">4.3.1.1 </span>Scripting languages</h5><p>The following lists some <a href="infrastructure.html#mime-type" id="mime-type_5">MIME type</a> strings and the
1.1 mike 347: languages to which they refer:<dl><dt>"<code>application/ecmascript</code>"</dt>
348: <dt>"<code>application/javascript</code>"</dt>
349: <dt>"<code>application/x-ecmascript</code>"</dt>
350: <dt>"<code>application/x-javascript</code>"</dt>
351: <dt>"<code>text/ecmascript</code>"</dt>
352: <dt>"<code>text/javascript</code>"</dt>
353: <dt>"<code>text/javascript1.0</code>"</dt>
354: <dt>"<code>text/javascript1.1</code>"</dt>
355: <dt>"<code>text/javascript1.2</code>"</dt>
356: <dt>"<code>text/javascript1.3</code>"</dt>
357: <dt>"<code>text/javascript1.4</code>"</dt>
358: <dt>"<code>text/javascript1.5</code>"</dt>
359: <dt>"<code>text/jscript</code>"</dt>
360: <dt>"<code>text/livescript</code>"</dt>
361: <dt>"<code>text/x-ecmascript</code>"</dt>
362: <dt>"<code>text/x-javascript</code>"</dt>
1.114 mike 363: <dd>JavaScript. <a href="references.html#refsECMA262">[ECMA262]</a></dd>
1.1 mike 364:
365: <dt>"<code>text/javascript;e4x=1</code>"</dt>
1.114 mike 366: <dd>JavaScript with ECMAScript for XML. <a href="references.html#refsECMA357">[ECMA357]</a></dd>
1.1 mike 367:
1.130 ! mike 368: </dl><h5 id="restrictions-for-contents-of-script-elements"><span class="secno">4.3.1.2 </span><dfn aria-haspopup="true" role="button" tabindex="0">Restrictions for contents of <code>script</code> elements</dfn></h5><p>The <code><a class="full-spec-link" href="http://dev.w3.org/html5/spec/infrastructure.html#textcontent" title='Read about this "textContent" reference in the full HTML5 spec.'>textContent</a></code> of a <code><a href="#the-script-element" id="script_21">script</a></code> element
1.1 mike 369: must match the <code title="">script</code> production in the
1.114 mike 370: following ABNF, the character set for which is Unicode. <a href="references.html#refsABNF">[ABNF]</a><pre>script = data1 *( escape [ script-start data3 ] "-->" data1 ) [ escape ]
1.1 mike 371: escape = "<!--" data2 *( script-start data3 script-end data2 )
372:
373: data1 = <any string that doesn't contain a substring that matches not-data1>
374: not-data1 = "<!--"
375:
376: data2 = <any string that doesn't contain a substring that matches not-data2>
377: not-data2 = script-start / "-->"
378:
379: data3 = <any string that doesn't contain a substring that matches not-data3>
380: not-data3 = script-end / "-->"
381:
382: script-start = lt s c r i p t tag-end
383: script-end = lt slash s c r i p t tag-end
384:
385: lt = %x003C ; U+003C LESS-THAN SIGN character (<)
386: slash = %x002F ; U+002F SOLIDUS character (/)
387:
388: s = %x0053 ; U+0053 LATIN CAPITAL LETTER S
389: s =/ %x0073 ; U+0073 LATIN SMALL LETTER S
390: c = %x0043 ; U+0043 LATIN CAPITAL LETTER C
391: c =/ %x0063 ; U+0063 LATIN SMALL LETTER C
392: r = %x0052 ; U+0052 LATIN CAPITAL LETTER R
393: r =/ %x0072 ; U+0072 LATIN SMALL LETTER R
394: i = %x0049 ; U+0049 LATIN CAPITAL LETTER I
395: i =/ %x0069 ; U+0069 LATIN SMALL LETTER I
396: p = %x0050 ; U+0050 LATIN CAPITAL LETTER P
397: p =/ %x0070 ; U+0070 LATIN SMALL LETTER P
398: t = %x0054 ; U+0054 LATIN CAPITAL LETTER T
399: t =/ %x0074 ; U+0074 LATIN SMALL LETTER T
400:
1.3 mike 401: tag-end = %x0009 ; U+0009 CHARACTER TABULATION (tab)
1.1 mike 402: tag-end =/ %x000A ; U+000A LINE FEED (LF)
403: tag-end =/ %x000C ; U+000C FORM FEED (FF)
404: tag-end =/ %x0020 ; U+0020 SPACE
405: tag-end =/ %x002F ; U+002F SOLIDUS (/)
1.130 ! mike 406: tag-end =/ %x003E ; U+003E GREATER-THAN SIGN (>)</pre><p>When a <code><a href="#the-script-element" id="script_22">script</a></code> element contains <a href="#inline-documentation-for-external-scripts" id="script-documentation_0">script
1.1 mike 407: documentation</a>, there are further restrictions on the contents
1.130 ! mike 408: of the element, as described in the section below.<h5 id="inline-documentation-for-external-scripts"><span class="secno">4.3.1.3 </span><dfn aria-haspopup="true" role="button" tabindex="0">Inline documentation for external scripts</dfn></h5><p>If a <code><a href="#the-script-element" id="script_23">script</a></code> element's <code title="attr-script-src"><a href="#attr-script-src" id="src_9">src</a></code> attribute is specified, then the
! 409: contents of the <code><a href="#the-script-element" id="script_24">script</a></code> element, if any, must be such
1.91 mike 410: that the value of the <code title="dom-script-text"><a href="#dom-script-text">text</a></code> IDL
1.1 mike 411: attribute, which is derived from the element's contents, matches the
412: <code title="">documentation</code> production in the following
1.114 mike 413: ABNF, the character set for which is Unicode. <a href="references.html#refsABNF">[ABNF]</a><pre>documentation = *( *( space / tab / comment ) [ line-comment ] newline )
1.1 mike 414: comment = slash star *( not-star / star not-slash ) 1*star slash
415: line-comment = slash slash *not-newline
416:
417: ; characters
1.3 mike 418: tab = %x0009 ; U+0009 CHARACTER TABULATION (tab)
1.1 mike 419: newline = %x000A ; U+000A LINE FEED (LF)
420: space = %x0020 ; U+0020 SPACE
421: star = %x002A ; U+002A ASTERISK (*)
422: slash = %x002F ; U+002F SOLIDUS (/)
423: not-newline = %x0000-0009 / %x000B-10FFFF
1.124 mike 424: ; a <a href="infrastructure.html#unicode-character" id="unicode-character_1">Unicode character</a> other than U+000A LINE FEED (LF)
1.1 mike 425: not-star = %x0000-0029 / %x002B-10FFFF
1.124 mike 426: ; a <a href="infrastructure.html#unicode-character" id="unicode-character_2">Unicode character</a> other than U+002A ASTERISK (*)
1.1 mike 427: not-slash = %x0000-002E / %x0030-10FFFF
1.124 mike 428: ; a <a href="infrastructure.html#unicode-character" id="unicode-character_3">Unicode character</a> other than U+002F SOLIDUS (/)</pre><p class="note">This corresponds to putting the contents of the
1.1 mike 429: element in JavaScript comments.<p class="note">This requirement is in addition to the earlier
1.130 ! mike 430: restrictions on the syntax of contents of <code><a href="#the-script-element" id="script_25">script</a></code>
1.1 mike 431: elements.<div class="example">
432:
433: <p>This allows authors to include documentation, such as license
434: information or API information, inside their documents while still
435: referring to external script files. The syntax is constrained so
436: that authors don't accidentally include what looks like valid
1.118 mike 437: script while also providing a <code title="attr-script-src"><a href="#attr-script-src" id="src_10">src</a></code> attribute.</p>
1.1 mike 438:
439: <pre><script src="cool-effects.js">
440: // create new instances using:
441: // var e = new Effect();
442: // start the effect using .play, stop using .stop:
443: // e.play();
444: // e.stop();
445: </script></pre>
446:
447: </div>
Webmaster