Another abandoned server code base... this is kind of an ancestor of taskrambler.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1552 lines
91 KiB

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en-US-x-Hixie" ><head><title>4.3 Scripting &#8212; HTML5 </title><style type="text/css">
pre { margin-left: 2em; white-space: pre-wrap; }
h2 { margin: 3em 0 1em 0; }
h3 { margin: 2.5em 0 1em 0; }
h4 { margin: 2.5em 0 0.75em 0; }
h5, h6 { margin: 2.5em 0 1em; }
h1 + h2, h1 + h2 + h2 { margin: 0.75em 0 0.75em; }
h2 + h3, h3 + h4, h4 + h5, h5 + h6 { margin-top: 0.5em; }
p { margin: 1em 0; }
hr:not(.top) { display: block; background: none; border: none; padding: 0; margin: 2em 0; height: auto; }
dl, dd { margin-top: 0; margin-bottom: 0; }
dt { margin-top: 0.75em; margin-bottom: 0.25em; clear: left; }
dt + dt { margin-top: 0; }
dd dt { margin-top: 0.25em; margin-bottom: 0; }
dd p { margin-top: 0; }
dd dl + p { margin-top: 1em; }
dd table + p { margin-top: 1em; }
p + * > li, dd li { margin: 1em 0; }
dt, dfn { font-weight: bold; font-style: normal; }
dt dfn { font-style: italic; }
pre, code { font-size: inherit; font-family: monospace; font-variant: normal; }
pre strong { color: black; font: inherit; font-weight: bold; background: yellow; }
pre em { font-weight: bolder; font-style: normal; }
@media screen { code { color: orangered; } code :link, code :visited { color: inherit; } }
var sub { vertical-align: bottom; font-size: smaller; position: relative; top: 0.1em; }
table { border-collapse: collapse; border-style: hidden hidden none hidden; }
table thead, table tbody { border-bottom: solid; }
table tbody th:first-child { border-left: solid; }
table tbody th { text-align: left; }
table td, table th { border-left: solid; border-right: solid; border-bottom: solid thin; vertical-align: top; padding: 0.2em; }
blockquote { margin: 0 0 0 2em; border: 0; padding: 0; font-style: italic; }
.bad, .bad *:not(.XXX) { color: gray; border-color: gray; background: transparent; }
.matrix, .matrix td { border: none; text-align: right; }
.matrix { margin-left: 2em; }
.dice-example { border-collapse: collapse; border-style: hidden solid solid hidden; border-width: thin; margin-left: 3em; }
.dice-example caption { width: 30em; font-size: smaller; font-style: italic; padding: 0.75em 0; text-align: left; }
.dice-example td, .dice-example th { border: solid thin; width: 1.35em; height: 1.05em; text-align: center; padding: 0; }
.toc dfn, h1 dfn, h2 dfn, h3 dfn, h4 dfn, h5 dfn, h6 dfn { font: inherit; }
img.extra { float: right; }
pre.idl { border: solid thin; background: #EEEEEE; color: black; padding: 0.5em 1em; }
pre.idl :link, pre.idl :visited { color: inherit; background: transparent; }
pre.css { border: solid thin; background: #FFFFEE; color: black; padding: 0.5em 1em; }
pre.css:first-line { color: #AAAA50; }
dl.domintro { color: green; margin: 2em 0 2em 2em; padding: 0.5em 1em; border: none; background: #DDFFDD; }
hr + dl.domintro, div.impl + dl.domintro { margin-top: 2.5em; margin-bottom: 1.5em; }
dl.domintro dt, dl.domintro dt * { color: black; text-decoration: none; }
dl.domintro dd { margin: 0.5em 0 1em 2em; padding: 0; }
dl.domintro dd p { margin: 0.5em 0; }
dl.switch { padding-left: 2em; }
dl.switch > dt { text-indent: -1.5em; }
dl.switch > dt:before { content: '\21AA'; padding: 0 0.5em 0 0; display: inline-block; width: 1em; text-align: right; line-height: 0.5em; }
dl.triple { padding: 0 0 0 1em; }
dl.triple dt, dl.triple dd { margin: 0; display: inline }
dl.triple dt:after { content: ':'; }
dl.triple dd:after { content: '\A'; white-space: pre; }
.diff-old { text-decoration: line-through; color: silver; background: transparent; }
.diff-chg, .diff-new { text-decoration: underline; color: green; background: transparent; }
a .diff-new { border-bottom: 1px blue solid; }
h2 { page-break-before: always; }
h1, h2, h3, h4, h5, h6 { page-break-after: avoid; }
h1 + h2, hr + h2.no-toc { page-break-before: auto; }
p > span:not([title=""]):not([class="XXX"]):not([class="impl"]):not([class="note"]),
li > span:not([title=""]):not([class="XXX"]):not([class="impl"]):not([class="note"]), { border-bottom: solid #9999CC; }
div.head { margin: 0 0 1em; padding: 1em 0 0 0; }
div.head p { margin: 0; }
div.head h1 { margin: 0; }
div.head .logo { float: right; margin: 0 1em; }
div.head .logo img { border: none } /* remove border from top image */
div.head dl { margin: 1em 0; }
div.head p.copyright, div.head p.alt { font-size: x-small; font-style: oblique; margin: 0; }
body > .toc > li { margin-top: 1em; margin-bottom: 1em; }
body > .toc.brief > li { margin-top: 0.35em; margin-bottom: 0.35em; }
body > .toc > li > * { margin-bottom: 0.5em; }
body > .toc > li > * > li > * { margin-bottom: 0.25em; }
.toc, .toc li { list-style: none; }
.brief { margin-top: 1em; margin-bottom: 1em; line-height: 1.1; }
.brief li { margin: 0; padding: 0; }
.brief li p { margin: 0; padding: 0; }
.category-list { margin-top: -0.75em; margin-bottom: 1em; line-height: 1.5; }
.category-list::before { content: '\21D2\A0'; font-size: 1.2em; font-weight: 900; }
.category-list li { display: inline; }
.category-list li:not(:last-child)::after { content: ', '; }
.category-list li > span, .category-list li > a { text-transform: lowercase; }
.category-list li * { text-transform: none; } /* don't affect <code> nested in <a> */
.XXX { color: #E50000; background: white; border: solid red; padding: 0.5em; margin: 1em 0; }
.XXX > :first-child { margin-top: 0; }
p .XXX { line-height: 3em; }
.annotation { border: solid thin black; background: #0C479D; color: white; position: relative; margin: 8px 0 20px 0; }
.annotation:before { position: absolute; left: 0; top: 0; width: 100%; height: 100%; margin: 6px -6px -6px 6px; background: #333333; z-index: -1; content: ''; }
.annotation :link, .annotation :visited { color: inherit; }
.annotation :link:hover, .annotation :visited:hover { background: transparent; }
.annotation span { border: none ! important; }
.note { color: green; background: transparent; font-family: sans-serif; }
.warning { color: red; background: transparent; }
.note, .warning { font-weight: bolder; font-style: italic; }
p.note, div.note { padding: 0.5em 2em; }
span.note { padding: 0 2em; }
.note p:first-child, .warning p:first-child { margin-top: 0; }
.note p:last-child, .warning p:last-child { margin-bottom: 0; }
.warning:before { font-style: normal; }
p.note:before { content: 'Note: '; }
p.warning:before { content: '\26A0 Warning! '; }
.bookkeeping:before { display: block; content: 'Bookkeeping details'; font-weight: bolder; font-style: italic; }
.bookkeeping { font-size: 0.8em; margin: 2em 0; }
.bookkeeping p { margin: 0.5em 2em; display: list-item; list-style: square; }
.bookkeeping dt { margin: 0.5em 2em 0; }
.bookkeeping dd { margin: 0 3em 0.5em; }
h4 { position: relative; z-index: 3; }
h4 + .element, h4 + div + .element { margin-top: -2.5em; padding-top: 2em; }
.element {
background: #EEEEFF;
color: black;
margin: 0 0 1em 0.15em;
padding: 0 1em 0.25em 0.75em;
border-left: solid #9999FF 0.25em;
position: relative;
z-index: 1;
}
.element:before {
position: absolute;
z-index: 2;
top: 0;
left: -1.15em;
height: 2em;
width: 0.9em;
background: #EEEEFF;
content: ' ';
border-style: none none solid solid;
border-color: #9999FF;
border-width: 0.25em;
}
.example { display: block; color: #222222; background: #FCFCFC; border-left: double; margin-left: 2em; padding-left: 1em; }
td > .example:only-child { margin: 0 0 0 0.1em; }
ul.domTree, ul.domTree ul { padding: 0 0 0 1em; margin: 0; }
ul.domTree li { padding: 0; margin: 0; list-style: none; position: relative; }
ul.domTree li li { list-style: none; }
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; }
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; }
ul.domTree span { font-style: italic; font-family: serif; }
ul.domTree .t1 code { color: purple; font-weight: bold; }
ul.domTree .t2 { font-style: normal; font-family: monospace; }
ul.domTree .t2 .name { color: black; font-weight: bold; }
ul.domTree .t2 .value { color: blue; font-weight: normal; }
ul.domTree .t3 code, .domTree .t4 code, .domTree .t5 code { color: gray; }
ul.domTree .t7 code, .domTree .t8 code { color: green; }
ul.domTree .t10 code { color: teal; }
body.dfnEnabled dfn { cursor: pointer; }
.dfnPanel {
display: inline;
position: absolute;
z-index: 10;
height: auto;
width: auto;
padding: 0.5em 0.75em;
font: small sans-serif, Droid Sans Fallback;
background: #DDDDDD;
color: black;
border: outset 0.2em;
}
.dfnPanel * { margin: 0; padding: 0; font: inherit; text-indent: 0; }
.dfnPanel :link, .dfnPanel :visited { color: black; }
.dfnPanel p { font-weight: bolder; }
.dfnPanel * + p { margin-top: 0.25em; }
.dfnPanel li { list-style-position: inside; }
#configUI { position: absolute; z-index: 20; top: 10em; right: 1em; width: 11em; font-size: small; }
#configUI p { margin: 0.5em 0; padding: 0.3em; background: #EEEEEE; color: black; border: inset thin; }
#configUI p label { display: block; }
#configUI #updateUI, #configUI .loginUI { text-align: center; }
#configUI input[type=button] { display: block; margin: auto; }
fieldset { margin: 1em; padding: 0.5em 1em; }
fieldset > legend + * { margin-top: 0; }
fieldset > :last-child { margin-bottom: 0; }
fieldset p { margin: 0.5em 0; }
.stability {
position: fixed;
bottom: 0;
left: 0; right: 0;
margin: 0 auto 0 auto !important;
z-index: 1000;
width: 50%;
background: maroon; color: yellow;
-webkit-border-radius: 1em 1em 0 0;
-moz-border-radius: 1em 1em 0 0;
border-radius: 1em 1em 0 0;
-moz-box-shadow: 0 0 1em #500;
-webkit-box-shadow: 0 0 1em #500;
box-shadow: 0 0 1em red;
padding: 0.5em 1em;
text-align: center;
}
.stability strong {
display: block;
}
.stability input {
appearance: none; margin: 0; border: 0; padding: 0.25em 0.5em; background: transparent; color: black;
position: absolute; top: -0.5em; right: 0; font: 1.25em sans-serif; text-align: center;
}
.stability input:hover {
color: white;
text-shadow: 0 0 2px black;
}
.stability input:active {
padding: 0.3em 0.45em 0.2em 0.55em;
}
.stability :link, .stability :visited,
.stability :link:hover, .stability :visited:hover {
background: transparent;
color: white;
}
</style><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"><link href="http://www.w3.org/StyleSheets/TR/W3C-WD" rel="stylesheet" type="text/css"><style type="text/css">
.applies thead th > * { display: block; }
.applies thead code { display: block; }
.applies tbody th { whitespace: nowrap; }
.applies td { text-align: center; }
.applies .yes { background: yellow; }
.matrix, .matrix td { border: hidden; text-align: right; }
.matrix { margin-left: 2em; }
.dice-example { border-collapse: collapse; border-style: hidden solid solid hidden; border-width: thin; margin-left: 3em; }
.dice-example caption { width: 30em; font-size: smaller; font-style: italic; padding: 0.75em 0; text-align: left; }
.dice-example td, .dice-example th { border: solid thin; width: 1.35em; height: 1.05em; text-align: center; padding: 0; }
td.eg { border-width: thin; text-align: center; }
#table-example-1 { border: solid thin; border-collapse: collapse; margin-left: 3em; }
#table-example-1 * { font-family: "Essays1743", serif; line-height: 1.01em; }
#table-example-1 caption { padding-bottom: 0.5em; }
#table-example-1 thead, #table-example-1 tbody { border: none; }
#table-example-1 th, #table-example-1 td { border: solid thin; }
#table-example-1 th { font-weight: normal; }
#table-example-1 td { border-style: none solid; vertical-align: top; }
#table-example-1 th { padding: 0.5em; vertical-align: middle; text-align: center; }
#table-example-1 tbody tr:first-child td { padding-top: 0.5em; }
#table-example-1 tbody tr:last-child td { padding-bottom: 1.5em; }
#table-example-1 tbody td:first-child { padding-left: 2.5em; padding-right: 0; width: 9em; }
#table-example-1 tbody td:first-child::after { content: leader(". "); }
#table-example-1 tbody td { padding-left: 2em; padding-right: 2em; }
#table-example-1 tbody td:first-child + td { width: 10em; }
#table-example-1 tbody td:first-child + td ~ td { width: 2.5em; }
#table-example-1 tbody td:first-child + td + td + td ~ td { width: 1.25em; }
.apple-table-examples { border: none; border-collapse: separate; border-spacing: 1.5em 0em; width: 40em; margin-left: 3em; }
.apple-table-examples * { font-family: "Times", serif; }
.apple-table-examples td, .apple-table-examples th { border: none; white-space: nowrap; padding-top: 0; padding-bottom: 0; }
.apple-table-examples tbody th:first-child { border-left: none; width: 100%; }
.apple-table-examples thead th:first-child ~ th { font-size: smaller; font-weight: bolder; border-bottom: solid 2px; text-align: center; }
.apple-table-examples tbody th::after, .apple-table-examples tfoot th::after { content: leader(". ") }
.apple-table-examples tbody th, .apple-table-examples tfoot th { font: inherit; text-align: left; }
.apple-table-examples td { text-align: right; vertical-align: top; }
.apple-table-examples.e1 tbody tr:last-child td { border-bottom: solid 1px; }
.apple-table-examples.e1 tbody + tbody tr:last-child td { border-bottom: double 3px; }
.apple-table-examples.e2 th[scope=row] { padding-left: 1em; }
.apple-table-examples sup { line-height: 0; }
.details-example img { vertical-align: top; }
#base64-table {
white-space: nowrap;
font-size: 0.6em;
column-width: 6em;
column-count: 5;
column-gap: 1em;
-moz-column-width: 6em;
-moz-column-count: 5;
-moz-column-gap: 1em;
-webkit-column-width: 6em;
-webkit-column-count: 5;
-webkit-column-gap: 1em;
}
#base64-table thead { display: none; }
#base64-table * { border: none; }
#base64-table tbody td:first-child:after { content: ':'; }
#base64-table tbody td:last-child { text-align: right; }
#named-character-references-table {
white-space: nowrap;
font-size: 0.6em;
column-width: 30em;
column-gap: 1em;
-moz-column-width: 30em;
-moz-column-gap: 1em;
-webkit-column-width: 30em;
-webkit-column-gap: 1em;
}
#named-character-references-table > table > tbody > tr > td:first-child + td,
#named-character-references-table > table > tbody > tr > td:last-child { text-align: center; }
#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; }
#named-character-references-table > table > tbody > tr#entity-CounterClockwiseContourIntegral > td:first-child { font-size: 0.5em; }
.glyph.control { color: red; }
@font-face {
font-family: 'Essays1743';
src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743.ttf');
}
@font-face {
font-family: 'Essays1743';
font-weight: bold;
src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743-Bold.ttf');
}
@font-face {
font-family: 'Essays1743';
font-style: italic;
src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743-Italic.ttf');
}
@font-face {
font-family: 'Essays1743';
font-style: italic;
font-weight: bold;
src: url('http://www.whatwg.org/specs/web-apps/current-work/fonts/Essays1743-BoldItalic.ttf');
}
</style><style type="text/css">
.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; }
</style><script type="text/javascript">
function getCookie(name) {
var params = location.search.substr(1).split("&");
for (var index = 0; index < params.length; index++) {
if (params[index] == name)
return "1";
var data = params[index].split("=");
if (data[0] == name)
return unescape(data[1]);
}
var cookies = document.cookie.split("; ");
for (var index = 0; index < cookies.length; index++) {
var data = cookies[index].split("=");
if (data[0] == name)
return unescape(data[1]);
}
return null;
}
</script>
<script src="link-fixup.js" type="text/javascript"></script>
<link href="style.css" rel="stylesheet"><link href="semantics.html" title="4 The elements of HTML" rel="prev">
<link href="spec.html#contents" title="Table of contents" rel="index">
<link href="sections.html" title="4.4 Sections" rel="next">
</head><body><div class="head" id="head">
<div id="multipage-common">
<p class="stability" id="wip"><strong>This is a work in
progress!</strong> For the latest updates from the HTML WG, possibly
including important bug fixes, please look at the <a href="http://dev.w3.org/html5/spec/Overview.html">editor's draft</a> instead.
There may also be a more
<a href="http://www.w3.org/TR/html5">up-to-date Working Draft</a>
with changes based on resolution of Last Call issues.
<input onclick="closeWarning(this.parentNode)" type="button" value="&#9587;&#8413;"></p>
<script type="text/javascript">
function closeWarning(element) {
element.parentNode.removeChild(element);
var date = new Date();
date.setDate(date.getDate()+4);
document.cookie = 'hide-obsolescence-warning=1; expires=' + date.toGMTString();
}
if (getCookie('hide-obsolescence-warning') == '1')
setTimeout(function () { document.getElementById('wip').parentNode.removeChild(document.getElementById('wip')); }, 2000);
</script></div>
<p><a href="http://www.w3.org/"><img alt="W3C" height="48" src="http://www.w3.org/Icons/w3c_home" width="72"></a></p>
<h1>HTML5</h1>
</div><div>
<a href="semantics.html" class="prev">4 The elements of HTML</a> &#8211;
<a href="spec.html#contents">Table of contents</a> &#8211;
<a href="sections.html" class="next">4.4 Sections</a>
<ol class="toc"><li><ol><li><a href="scripting-1.html#scripting-1"><span class="secno">4.3 </span>Scripting</a>
<ol><li><a href="scripting-1.html#the-script-element"><span class="secno">4.3.1 </span>The <code>script</code> element</a>
<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><li><a href="scripting-1.html#scriptTagXSLT"><span class="secno">4.3.1.4 </span>Interaction of <code>script</code> elements and XSLT</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>
<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
scripting where possible, as declarative mechanisms are often more
maintainable, and many users disable scripting.</p><div class="example">
<p>For example, instead of using script to show or hide a section
to show more details, the <code><a href="interactive-elements.html#the-details-element">details</a></code> element could be
used.</p>
</div><p>Authors are also encouraged to make their applications degrade
gracefully in the absence of scripting support.</p><div class="example">
<p>For example, if an author provides a link in a table header to
dynamically resort the table, the link could also be made to
function without scripts by requesting the sorted table from the
server.</p>
</div><h4 id="the-script-element"><span class="secno">4.3.1 </span>The <dfn id="script"><code>script</code></dfn> element</h4><dl class="element"><dt>Categories</dt>
<dd><a href="content-models.html#metadata-content">Metadata content</a>.</dd>
<dd><a href="content-models.html#flow-content">Flow content</a>.</dd>
<dd><a href="content-models.html#phrasing-content">Phrasing content</a>.</dd>
<dt>Contexts in which this element can be used:</dt>
<dd>Where <a href="content-models.html#metadata-content">metadata content</a> is expected.</dd>
<dd>Where <a href="content-models.html#phrasing-content">phrasing content</a> is expected.</dd>
<dt>Content model:</dt>
<dd>If there is no <code title="attr-script-src"><a href="#attr-script-src">src</a></code>
attribute, depends on the value of the <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute, but must match
<a href="#restrictions-for-contents-of-script-elements">script content restrictions</a>.</dd>
<dd>If there <em>is</em> a <code title="attr-script-src"><a href="#attr-script-src">src</a></code>
attribute, the element must be either empty or contain only
<a href="#inline-documentation-for-external-scripts">script documentation</a> that also matches <a href="#restrictions-for-contents-of-script-elements">script
content restrictions</a>.</dd>
<dt>Content attributes:</dt>
<dd><a href="elements.html#global-attributes">Global attributes</a></dd>
<dd><code title="attr-script-src"><a href="#attr-script-src">src</a></code></dd>
<dd><code title="attr-script-async"><a href="#attr-script-async">async</a></code></dd>
<dd><code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code></dd>
<dd><code title="attr-script-type"><a href="#attr-script-type">type</a></code></dd>
<dd><code title="attr-script-charset"><a href="#attr-script-charset">charset</a></code></dd>
<dt>DOM interface:</dt>
<dd>
<pre class="idl">interface <dfn id="htmlscriptelement">HTMLScriptElement</dfn> : <a href="elements.html#htmlelement">HTMLElement</a> {
attribute DOMString <a href="#dom-script-src" title="dom-script-src">src</a>;
attribute boolean <a href="#dom-script-async" title="dom-script-async">async</a>;
attribute boolean <a href="#dom-script-defer" title="dom-script-defer">defer</a>;
attribute DOMString <a href="#dom-script-type" title="dom-script-type">type</a>;
attribute DOMString <a href="#dom-script-charset" title="dom-script-charset">charset</a>;
attribute DOMString <a href="#dom-script-text" title="dom-script-text">text</a>;
};</pre>
</dd>
</dl><p>The <code><a href="#the-script-element">script</a></code> element allows authors to include dynamic
script and data blocks in their documents. The element does not
<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
embedded inline or may be imported from an external file using the
<code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute. If the language
is not that described by "<code title="">text/javascript</code>",
then the <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute must
be present, as described below. Whatever language is used, the
contents of the <code><a href="#the-script-element">script</a></code> element must conform with the
requirements of that language's specification.</p><p>When used to include data blocks (as opposed to scripts), the
data must be embedded inline, the format of the data must be given
using the <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute, the
<code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute must not be
specified, and the contents of the <code><a href="#the-script-element">script</a></code> element must
conform to the requirements defined for the format used.</p><p>The <dfn id="attr-script-type" title="attr-script-type"><code>type</code></dfn>
attribute gives the language of the script or format of the data. If
the attribute is present, its value must be a <a href="infrastructure.html#valid-mime-type">valid MIME
type</a>. The <code title="">charset</code> parameter must not be
specified. The default, which is used if the attribute is absent,
is "<code title="">text/javascript</code>".</p><p>The <dfn id="attr-script-src" title="attr-script-src"><code>src</code></dfn>
attribute, if specified, gives the address of the external script
resource to use. The value of the attribute must be a <a href="urls.html#valid-non-empty-url-potentially-surrounded-by-spaces">valid
non-empty URL potentially surrounded by spaces</a> identifying a
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
present, or of the type "<code title="">text/javascript</code>", if
the attribute is absent. A resource is a script resource of a given
type if that type identifies a scripting language and the resource
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>
attribute gives the character encoding of the external script
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
attribute is set, its value must be a valid character encoding name,
must be an <a href="infrastructure.html#ascii-case-insensitive">ASCII case-insensitive</a> match for the
<a href="infrastructure.html#preferred-mime-name">preferred MIME name</a> for that encoding, and must match
the encoding given in the <code title="">charset</code> parameter of
the <a href="fetching-resources.html#content-type" title="Content-Type">Content-Type metadata</a> of the
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
<dfn id="attr-script-defer" title="attr-script-defer"><code>defer</code></dfn> attributes
are <a href="common-microsyntaxes.html#boolean-attribute" title="boolean attribute">boolean attributes</a> that
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
specified if the <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute
is not present.</p><p>There are three possible modes that can be selected using these
attributes. If the <code title="attr-script-async"><a href="#attr-script-async">async</a></code>
attribute is present, then the script will be executed
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
the <code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code> attribute is
present, then the script is executed when the page has finished
parsing. If neither attribute is present, then the script is
fetched and executed immediately, before the user agent continues
parsing the page.</p><p class="note">The exact processing details for these attributes
are, for mostly historical reasons, somewhat non-trivial, involving
a number of aspects of HTML. The implementation requirements are
therefore by necessity scattered throughout the specification. The
algorithms below (in this section) describe the core of this
processing, but these algorithms reference and are referenced by the
parsing rules for <code><a href="#the-script-element">script</a></code> <a href="tree-construction.html#scriptTag">start</a>
and <a href="tree-construction.html#scriptEndTag">end</a> tags in HTML, <a href="tree-construction.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
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
specified even if the <code title="attr-script-async"><a href="#attr-script-async">async</a></code>
attribute is specified, to cause legacy Web browsers that only
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
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
direct effect; these attribute are only used at specific times
described below.</p><div class="impl">
<p>A <code><a href="#the-script-element">script</a></code> element has several associated pieces of
state.</p>
<p>The first is a flag indicating whether or not the script block
has been <dfn id="already-started">"already started"</dfn>. Initially,
<code><a href="#the-script-element">script</a></code> elements must have this flag unset (script
blocks, when created, are not "already started"). When a
<code><a href="#the-script-element">script</a></code> element is <a href="infrastructure.html#concept-clone" title="concept-clone">cloned</a>, the "already started" flag, if
set, must be propagated to the clone when it is created.</p>
<p>The second is a flag indicating whether the element was
<dfn id="parser-inserted">"parser-inserted"</dfn>. Initially, <code><a href="#the-script-element">script</a></code>
elements must have this flag unset. It is set by the <a href="parsing.html#html-parser">HTML
parser</a> and the <a href="the-xhtml-syntax.html#xml-parser">XML parser</a> on <code><a href="#the-script-element">script</a></code>
elements they insert and affects the processing of those
elements.</p>
<p>The third is a flag indicating whether the element will
<dfn id="force-async">"force-async"</dfn>. Initially, <code><a href="#the-script-element">script</a></code> elements
must have this flag set. It is unset by the <a href="parsing.html#html-parser">HTML parser</a>
and the <a href="the-xhtml-syntax.html#xml-parser">XML parser</a> on <code><a href="#the-script-element">script</a></code> elements they
insert. In addition, whenever a <code><a href="#the-script-element">script</a></code> element whose
<a href="#force-async">"force-async"</a> flag is set has a <code title="attr-script-async"><a href="#attr-script-async">async</a></code> content attribute added, the
element's <a href="#force-async">"force-async"</a> flag must be unset.</p>
<p>The fourth is a flag indicating whether or not the script block is
<dfn id="ready-to-be-parser-executed">"ready to be parser-executed"</dfn>. Initially,
<code><a href="#the-script-element">script</a></code> elements must have this flag unset (script
blocks, when created, are not "ready to be parser-executed"). This
flag is used only for elements that are also
<a href="#parser-inserted">"parser-inserted"</a>, to let the parser know when to
execute the script.</p>
<p>The last few pieces of state are <dfn id="the-script-block-s-type"><var>the script block's
type</var></dfn>, <dfn id="the-script-block-s-character-encoding"><var>the script block's character
encoding</var></dfn>, and <dfn id="the-script-block-s-fallback-character-encoding"><var>the script block's fallback
character encoding</var></dfn>. They are determined when the script
is prepared, based on the attributes on the element at that time,
and the <code><a href="infrastructure.html#document">Document</a></code> of the <code><a href="#the-script-element">script</a></code>
element.</p>
<p>When a <code><a href="#the-script-element">script</a></code> element that is not marked as being
<a href="#parser-inserted">"parser-inserted"</a> experiences one of the events listed
in the following list, the user agent must synchronously <a href="#prepare-a-script" title="prepare a script">prepare</a> the <code><a href="#the-script-element">script</a></code>
element:</p>
<ul><li>The <code><a href="#the-script-element">script</a></code> element gets <a href="infrastructure.html#insert-an-element-into-a-document" title="insert an
element into a document">inserted into a document</a>.</li>
<li>The <code><a href="#the-script-element">script</a></code> element is <a href="infrastructure.html#in-a-document">in a
<code>Document</code></a> and its child nodes are changed.</li>
<li>The <code><a href="#the-script-element">script</a></code> element is <a href="infrastructure.html#in-a-document">in a
<code>Document</code></a> and has a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute set where previously
the element had no such attribute.</li>
</ul><p>To <dfn id="prepare-a-script" title="prepare a script">prepare a script</dfn>, the user
agent must act as follows:</p>
<ol><li>
<p>If the <code><a href="#the-script-element">script</a></code> element is marked as having
<a href="#already-started">"already started"</a>, then the user agent must abort
these steps at this point. The script is not executed.</p>
</li>
<li>
<p>If the element has its <a href="#parser-inserted">"parser-inserted"</a> flag set,
then set <var title="">was-parser-inserted</var> to true and unset
the element's <a href="#parser-inserted">"parser-inserted"</a> flag. Otherwise, set
<var title="">was-parser-inserted</var> to false.</p>
<p class="note">This is done so that if parser-inserted
<code><a href="#the-script-element">script</a></code> elements fail to run when the parser tries to
run them, e.g. because they are empty or specify an unsupported
scripting language, another script can later mutate them and cause
them to run again.</p>
</li>
<li>
<p>If <var title="">was-parser-inserted</var> is true and the
element does not have an <code title="attr-script-async"><a href="#attr-script-async">async</a></code> attribute, then set the
element's <a href="#force-async">"force-async"</a> flag to true.</p>
<p class="note">This is done so that if a parser-inserted
<code><a href="#the-script-element">script</a></code> element fails to run when the parser tries to
run it, but it is later executed after a script dynamically
updates it, it will execute asynchronously even if the
<code title="attr-script-async"><a href="#attr-script-async">async</a></code> attribute isn't
set.</p>
</li>
<li id="script-processing-empty">
<p>If the element has no <code title="attr-script-src"><a href="#attr-script-src">src</a></code>
attribute, and its child nodes, if any, consist only of comment
nodes and empty <a href="infrastructure.html#text-node" title="text node">text nodes</a>, then
the user agent must abort these steps at this point. The script is
not executed.</p>
</li>
<li>
<p>If the element is not <a href="infrastructure.html#in-a-document">in a <code>Document</code></a>,
then the user agent must abort these steps at this point. The
script is not executed.</p>
</li>
<li id="script-processing-prepare">
<p>If either:</p>
<ul class="brief"><li>the <code><a href="#the-script-element">script</a></code> element has a <code title="attr-script-type"><a href="#attr-script-type">type</a></code> attribute and its value is
the empty string, or</li>
<li>the <code><a href="#the-script-element">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
<em>that</em> attribute's value is the empty string, or</li>
<li>the <code><a href="#the-script-element">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>
</ul><p>...let <var><a href="#the-script-block-s-type">the script block's type</a></var> for this
<code><a href="#the-script-element">script</a></code> element be "<code title="">text/javascript</code>".</p>
<p>Otherwise, if the <code><a href="#the-script-element">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
script block's type</a></var> for this <code><a href="#the-script-element">script</a></code> element be
the value of that attribute with any leading or trailing sequences
of <a href="common-microsyntaxes.html#space-character" title="space character">space characters</a>
removed.</p>
<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
<var><a href="#the-script-block-s-type">the script block's type</a></var> for this <code><a href="#the-script-element">script</a></code>
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>
<p class="note">The <code title="attr-script-language"><a href="obsolete.html#attr-script-language">language</a></code> attribute is never
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>
</li>
<li>
<p>If the user agent does not <a href="#support-the-scripting-language">support the scripting
language</a> given by <var><a href="#the-script-block-s-type">the script block's type</a></var> for
this <code><a href="#the-script-element">script</a></code> element, then the user agent must abort
these steps at this point. The script is not executed.</p>
</li>
<li>
<p>If <var title="">was-parser-inserted</var> is true, then flag
the element as <a href="#parser-inserted">"parser-inserted"</a> again, and set the
element's <a href="#force-async">"force-async"</a> flag to false.</p>
</li>
<li id="script-processing-start">
<p>The user agent must set the element's <a href="#already-started">"already
started"</a> flag.</p>
<p class="note">The state of the element at this moment <a href="#establish-script-block-source">is later used</a> to
determine the script source.</p>
</li>
<li>
<p>If the element is flagged as <a href="#parser-inserted">"parser-inserted"</a>,
but the element's <code><a href="infrastructure.html#document">Document</a></code> is not the
<code><a href="infrastructure.html#document">Document</a></code> of the parser that created the element, then
abort these steps.</p>
</li>
<li id="script-processing-noscript">
<p>If <a href="webappapis.html#concept-n-noscript" title="concept-n-noscript">scripting is
disabled</a> for the <code><a href="#the-script-element">script</a></code> element, then the user
agent must abort these steps at this point. The script is not
executed.</p>
<p class="note">The definition of <a href="webappapis.html#concept-n-noscript" title="concept-n-noscript">scripting is disabled</a> means
that, amongst others, the following scripts will not execute:
scripts in <code>XMLHttpRequest</code>'s <code title="dom-XMLHttpRequest-responseXML">responseXML</code>
documents, scripts in <code>DOMParser</code>-created documents,
scripts in documents created by <code>XSLTProcessor</code>'s <code title="dom-XSLTProcessor-transformToDocument">transformToDocument</code>
feature, and scripts that are first inserted by a script into a
<code><a href="infrastructure.html#document">Document</a></code> that was created using the <code title="dom-DOMImplementation-createDocument"><a href="infrastructure.html#dom-domimplementation-createdocument">createDocument()</a></code>
API. <a href="references.html#refsXHR">[XHR]</a> <a href="references.html#refsDOMPARSER">[DOMPARSER]</a> <a href="references.html#refsDOMCORE">[DOMCORE]</a></p>
</li>
<li id="script-processing-for">
<p>If the <code><a href="#the-script-element">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
substeps:</p>
<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>
<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>
<li><p><a href="common-microsyntaxes.html#strip-leading-and-trailing-whitespace">Strip leading and trailing whitespace</a> from
<var title="">event</var> and <var title="">for</var>.</p></li>
<li><p>If <var title="">for</var> is not an <a href="infrastructure.html#ascii-case-insensitive">ASCII
case-insensitive</a> match for the string "<code title="">window</code>", then the user agent must abort these
steps at this point. The script is not executed.</p></li>
<li><p>If <var title="">event</var> is not an <a href="infrastructure.html#ascii-case-insensitive">ASCII
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
steps at this point. The script is not executed.</p></li>
</ol></li>
<li id="script-processing-encoding">
<p>If the <code><a href="#the-script-element">script</a></code> element has a <code title="attr-script-charset"><a href="#attr-script-charset">charset</a></code> attribute, then let
<var><a href="#the-script-block-s-character-encoding">the script block's character encoding</a></var> for this
<code><a href="#the-script-element">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>
<p>Otherwise, let <var><a href="#the-script-block-s-fallback-character-encoding">the script block's fallback character
encoding</a></var> for this <code><a href="#the-script-element">script</a></code> element be the same as
<a href="dom.html#document-s-character-encoding" title="document's character encoding">the encoding of the
document itself</a>.</p>
<p class="note">Only one of these two pieces of state is set.</p>
</li>
<li id="script-processing-src-prepare">
<p>If the element has a <code title="attr-script-src"><a href="#attr-script-src">src</a></code>
attribute whose value is not the empty string, then the value of
that attribute must be <a href="urls.html#resolve-a-url" title="resolve a url">resolved</a>
relative to the element, and if that is successful, the specified
resource must then be <a href="fetching-resources.html#fetch" title="fetch">fetched</a>, from the
<a href="origin-0.html#origin">origin</a> of the element's <code><a href="infrastructure.html#document">Document</a></code>.</p>
<p>If the <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute's
value is the empty string or if it could not be resolved, then the
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
event</a> named <code title="event-error">error</code> at the
element, and abort these steps.</p>
<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>
URL</a>, then the user agent must not, despite the requirements
in the definition of the <a href="fetching-resources.html#fetch" title="fetch">fetching</a>
algorithm, actually execute the script in the URL; instead the
user agent must act as if it had received an empty HTTP 400
response.</p>
<p>For performance reasons, user agents may start fetching the
script as soon as the attribute is set, instead, in the hope that
the element will be inserted into the document. Either way, once
the element is <a href="infrastructure.html#insert-an-element-into-a-document" title="insert an element into a
document">inserted into the document</a>, the load must have
started. If the UA performs such prefetching, but the element is
never inserted in the document, or the <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute is dynamically
changed, then the
user agent will not execute the script, and the fetching process
will have been effectively wasted.</p>
</li>
<li>
<p>Then, the first of the following options that describes the
situation must be followed:</p>
<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
a <code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code> attribute, and the
element has been flagged as <a href="#parser-inserted">"parser-inserted"</a>, and
the element does not have an <code title="attr-script-async"><a href="#attr-script-async">async</a></code> attribute</dt>
<dd>
<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
scripts that will execute when the document has finished
parsing</dfn> associated with the <code><a href="infrastructure.html#document">Document</a></code> of the
parser that created the element.</p>
<p>The <a href="webappapis.html#concept-task" title="concept-task">task</a> that the
<a href="webappapis.html#networking-task-source">networking task source</a> places on the <a href="webappapis.html#task-queue">task
queue</a> once the <a href="fetching-resources.html#fetch" title="fetch">fetching
algorithm</a> has completed must set the element's
<a href="#ready-to-be-parser-executed">"ready to be parser-executed"</a> flag. The parser will
handle executing the script.</p>
</dd>
<dt id="script-processing-parser-inserted">If the element has a
<code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute, and the
element has been flagged as <a href="#parser-inserted">"parser-inserted"</a>, and
the element does not have an <code title="attr-script-async"><a href="#attr-script-async">async</a></code> attribute</dt>
<dd>
<p>The element is the <a href="#pending-parsing-blocking-script">pending parsing-blocking
script</a> of the <code><a href="infrastructure.html#document">Document</a></code> of the parser that
created the element. (There can only be one such script per
<code><a href="infrastructure.html#document">Document</a></code> at a time.)</p>
<p>The <a href="webappapis.html#concept-task" title="concept-task">task</a> that the
<a href="webappapis.html#networking-task-source">networking task source</a> places on the <a href="webappapis.html#task-queue">task
queue</a> once the <a href="fetching-resources.html#fetch" title="fetch">fetching
algorithm</a> has completed must set the element's
<a href="#ready-to-be-parser-executed">"ready to be parser-executed"</a> flag. The parser will
handle executing the script.</p>
</dd>
<dt id="script-processing-style-delayed">If the element does not
have a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute, and
the element has been flagged as <a href="#parser-inserted">"parser-inserted"</a>,
and the <code><a href="infrastructure.html#document">Document</a></code> of the <a href="parsing.html#html-parser">HTML parser</a> or
<a href="the-xhtml-syntax.html#xml-parser">XML parser</a> that created the <code><a href="#the-script-element">script</a></code>
element <a href="semantics.html#has-a-style-sheet-that-is-blocking-scripts">has a style sheet that is blocking
scripts</a></dt>
<dd>
<p>The element is the <a href="#pending-parsing-blocking-script">pending parsing-blocking
script</a> of the <code><a href="infrastructure.html#document">Document</a></code> of the parser that
created the element. (There can only be one such script per
<code><a href="infrastructure.html#document">Document</a></code> at a time.)</p>
<p>Set the element's <a href="#ready-to-be-parser-executed">"ready to be parser-executed"</a>
flag. The parser will handle executing the script.</p>
</dd>
<dt id="script-processing-src-sync">If the element has a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute, does not have an
<code title="attr-script-async"><a href="#attr-script-async">async</a></code> attribute, and does
not have the <a href="#force-async">"force-async"</a> flag set</dt>
<dd>
<p>The element must be added to the end of the <dfn id="list-of-scripts-that-will-execute-in-order-as-soon-as-possible">list of
scripts that will execute in order as soon as possible</dfn>
associated with the <code><a href="infrastructure.html#document">Document</a></code> of the
<code><a href="#the-script-element">script</a></code> element at the time the <a href="#prepare-a-script">prepare a
script</a> algorithm started.</p>
<p>The <a href="webappapis.html#concept-task" title="concept-task">task</a> that the
<a href="webappapis.html#networking-task-source">networking task source</a> places on the <a href="webappapis.html#task-queue">task
queue</a> once the <a href="fetching-resources.html#fetch" title="fetch">fetching
algorithm</a> has completed must run the following steps:</p>
<ol><li><p>If the element is not now the first element in the
<a href="#list-of-scripts-that-will-execute-in-order-as-soon-as-possible">list of scripts that will execute in order as soon as
possible</a> to which it was added above, then mark the
element as ready but abort these steps without executing the
script yet.</p></li>
<li><p><i>Execution</i>: <a href="#execute-the-script-block">Execute the script block</a>
corresponding to the first script element in this <a href="#list-of-scripts-that-will-execute-in-order-as-soon-as-possible">list of
scripts that will execute in order as soon as
possible</a>.</p></li>
<li><p>Remove the first element from this <a href="#list-of-scripts-that-will-execute-in-order-as-soon-as-possible">list of scripts
that will execute in order as soon as possible</a>.</p></li>
<li><p>If this <a href="#list-of-scripts-that-will-execute-in-order-as-soon-as-possible">list of scripts that will execute in order
as soon as possible</a> is still not empty and the first
entry has already been marked as ready, then jump back to the
step labeled <i>execution</i>.</p></li>
</ol></dd>
<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>
<dd>
<p>The element must be added to the <dfn id="set-of-scripts-that-will-execute-as-soon-as-possible">set of scripts that
will execute as soon as possible</dfn> of the
<code><a href="infrastructure.html#document">Document</a></code> of the <code><a href="#the-script-element">script</a></code> element at the
time the <a href="#prepare-a-script">prepare a script</a> algorithm started.</p>
<p>The <a href="webappapis.html#concept-task" title="concept-task">task</a> that the
<a href="webappapis.html#networking-task-source">networking task source</a> places on the <a href="webappapis.html#task-queue">task
queue</a> once the <a href="fetching-resources.html#fetch" title="fetch">fetching
algorithm</a> has completed must <a href="#execute-the-script-block">execute the script
block</a> and then remove the element from the <a href="#set-of-scripts-that-will-execute-as-soon-as-possible">set of
scripts that will execute as soon as possible</a>.</p>
</dd>
<dt id="script-processing-inline">Otherwise</dt>
<dd>The user agent must immediately <a href="#execute-the-script-block">execute the script
block</a>, even if other scripts are already executing.</dd>
</dl></li>
</ol><p>Fetching an external script must <a href="the-end.html#delay-the-load-event">delay the load
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
task">queued</a> by the <a href="webappapis.html#networking-task-source">networking task source</a> once
the resource has been <a href="fetching-resources.html#fetch" title="fetch">fetched</a> (defined
above) has been run.</p>
<p>The <dfn id="pending-parsing-blocking-script">pending parsing-blocking script</dfn> of a
<code><a href="infrastructure.html#document">Document</a></code> is used by the <code><a href="infrastructure.html#document">Document</a></code>'s
parser(s).</p>
<p class="note">If a <code><a href="#the-script-element">script</a></code> element that blocks a
parser gets moved to another <code><a href="infrastructure.html#document">Document</a></code> before it would
normally have stopped blocking that parser, it nonetheless continues
blocking that parser until the condition that causes it to be
blocking the parser no longer applies (e.g. if the script is a
<a href="#pending-parsing-blocking-script">pending parsing-blocking script</a> because there was
<a href="semantics.html#a-style-sheet-that-is-blocking-scripts">a style sheet that is blocking scripts</a> when it was
parsed, but then the script is moved to another
<code><a href="infrastructure.html#document">Document</a></code> before the style sheet loads, the script still
blocks the parser until the style sheets are all loaded, at which
time the script executes and the parser is unblocked).</p>
<p>When the user agent is required to <dfn id="execute-the-script-block" title="execute the script
block">execute a script block</dfn>, it must run the following
steps:</p>
<ol><li>
<p>If the element is flagged as <a href="#parser-inserted">"parser-inserted"</a>,
but the element's <code><a href="infrastructure.html#document">Document</a></code> is not the
<code><a href="infrastructure.html#document">Document</a></code> of the parser that created the element, then
abort these steps.</p>
</li>
<li>
<p>Jump to the appropriate set of steps from the list below:</p>
<dl class="switch"><dt>If the load resulted in an error (for example a DNS error, or
an HTTP 404 error)</dt>
<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
<code title="event-error">error</code> at the element.</p></dd>
<dt>If the load was successful</dt>
<!-- SCRIPT EXEC -->
<dd>
<p>Executing the script block must consist of running the
following steps. For the purposes of these steps, the script is
considered to be from an <i>external file</i> if, while the
<a href="#prepare-a-script">prepare a script</a> algorithm above was running for
this script, the <code><a href="#the-script-element">script</a></code> element had a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute specified.</p>
<ol><li id="establish-script-block-source">
<p>Initialize <dfn id="the-script-block-s-source"><var>the script block's source</var></dfn>
as follows:</p>
<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>
<dd>
<p>The contents of that file, interpreted as string of
Unicode characters, are the script source.</p>
<p>To obtain the string of Unicode characters, the user
agent run the following steps:</p>
<ol><li><p>If the resource's <a href="fetching-resources.html#content-type" title="Content-Type">Content
Type metadata</a>, if any, specifies a character
encoding, and the user agent supports that encoding, then
let <var title="">character encoding</var> be that
encoding, and jump to the bottom step in this series of
steps.</p></li>
<li><p>If the algorithm above set <var><a href="#the-script-block-s-character-encoding">the script block's
character encoding</a></var>, then let <var title="">character
encoding</var> be that encoding, and jump to the bottom
step in this series of steps.</p></li>
<li><p>For each of the rows in the following table,
starting with the first one and going down, if the file has
as many or more bytes available than the number of bytes in
the first column, and the first bytes of the file match the
bytes given in the first column, then set <var title="">character encoding</var> to the encoding given in
the cell in the second column of that row, and jump to the
bottom step in this series of steps:</p>
<table id="table-script-bom"><thead><tr><th>Bytes in Hexadecimal
</th><th>Encoding
</th></tr></thead><tbody><tr><td>FE FF
</td><td>Big-endian UTF-16
</td></tr><tr><td>FF FE
</td><td>Little-endian UTF-16
</td></tr><tr><td>EF BB BF
</td><td>UTF-8
</td></tr></tbody></table><p class="note">This step looks for Unicode Byte Order
Marks (BOMs).</p>
</li>
<li><p>Let <var title="">character encoding</var> be
<var><a href="#the-script-block-s-fallback-character-encoding">the script block's fallback character
encoding</a></var>.</p></li>
<li><p>Convert the file to Unicode using <var>character
encoding</var>, following the rules for doing so given by
the specification for <var><a href="#the-script-block-s-type">the script block's
type</a></var>.</p></li>
</ol></dd>
<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>
<dd>
<p>The external file is the script source. When it is later
executed, it must be interpreted in a manner consistent with
the specification defining the language given by <var><a href="#the-script-block-s-type">the
script block's type</a></var>.</p>
</dd>
<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>
<dd>
<p>The value of the <code title="dom-script-text"><a href="#dom-script-text">text</a></code> IDL attribute at the
time the element's <a href="#already-started">"already started"</a> flag was
last set is the script source.</p>
</dd>
<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>
<dd>
<p>The child nodes of the <code><a href="#the-script-element">script</a></code> element at the
time the element's <a href="#already-started">"already started"</a> flag was
last set are the script source.</p>
</dd>
</dl></li>
<li>
<p>If the script is from an external file, then increment the
<a href="apis-in-html-documents.html#ignore-destructive-writes-counter">ignore-destructive-writes counter</a> of the
<code><a href="#the-script-element">script</a></code> element's <code><a href="infrastructure.html#document">Document</a></code>. Let <var title="">neutralized doc</var> be that
<code><a href="infrastructure.html#document">Document</a></code>.</p>
</li>
<li>
<p><a href="webappapis.html#create-a-script-from-a-node" title="create a script from a node">Create a
script</a> from the <code><a href="#the-script-element">script</a></code> element node, using
<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
block's type</a></var>.</p>
<p class="note">This is where the script is compiled and
actually executed.</p>
</li>
<li>
<p>Decrement the <a href="apis-in-html-documents.html#ignore-destructive-writes-counter">ignore-destructive-writes
counter</a> of <var title="">neutralized doc</var>, if it
was incremented in the earlier step.</p>
</li>
<li>
<p>If the script is from an external file, <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="#the-script-element">script</a></code> element.</p>
<p>Otherwise, the script is internal; <a href="webappapis.html#queue-a-task">queue a
task</a> to <a href="webappapis.html#fire-a-simple-event">fire a simple event</a> named <code title="event-load">load</code> at the <code><a href="#the-script-element">script</a></code>
element.</p>
</li>
</ol></dd>
</dl></li>
</ol><p>The IDL attributes <dfn id="dom-script-src" title="dom-script-src"><code>src</code></dfn>, <dfn id="dom-script-type" title="dom-script-type"><code>type</code></dfn>, <dfn id="dom-script-charset" title="dom-script-charset"><code>charset</code></dfn>, and <dfn id="dom-script-defer" title="dom-script-defer"><code>defer</code></dfn>, each must
<a href="common-dom-interfaces.html#reflect">reflect</a> the respective content attributes of the same
name.</p>
<p>The <dfn id="dom-script-async" title="dom-script-async"><code>async</code></dfn> IDL
attribute controls whether the element will execute asynchronously
or not. If the element's <a href="#force-async">"force-async"</a> flag is set,
then, on getting, the <code title="dom-script-async"><a href="#dom-script-async">async</a></code>
IDL attribute must return true, and on setting, the
<a href="#force-async">"force-async"</a> flag must first be unset, and then the
content attribute must be removed if the IDL attribute's new value
is false, and must be set to the empty string if the IDL attribute's
new value is true. If the element's <a href="#force-async">"force-async"</a> flag
is <em>not</em> set, the IDL attribute must <a href="common-dom-interfaces.html#reflect">reflect</a> the
<code title="attr-script-async"><a href="#attr-script-async">async</a></code> content attribute.</p>
</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>
<dd>
<p>Returns the contents of the element, ignoring child nodes that
aren't <a href="infrastructure.html#text-node" title="text node">text nodes</a>.</p>
<p>Can be set, to replace the element's children with the given
value.</p>
</dd>
</dl><div class="impl">
<p>The IDL attribute <dfn id="dom-script-text" title="dom-script-text"><code>text</code></dfn> must return a
concatenation of the contents of all the <a href="infrastructure.html#text-node" title="text
node">text nodes</a> that are direct children of the
<code><a href="#the-script-element">script</a></code> element (ignoring any other nodes such as
comments or elements), in tree order. On setting, it must act the
same way as the <code><a href="infrastructure.html#textcontent">textContent</a></code> IDL attribute.</p>
</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,
<code><a href="#the-script-element">script</a></code> elements execute (typically synchronously), but
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
execute at all.</p><div class="example">
<p>In this example, two <code><a href="#the-script-element">script</a></code> elements are used. One
embeds an external script, and the other includes some data.</p>
<pre>&lt;script src="game-engine.js"&gt;&lt;/script&gt;
&lt;script type="text/x-game-map"&gt;
........U.........e
o............A....e
.....A.....AAA....e
.A..AAA...AAAAA...e
&lt;/script&gt;</pre>
<p>The data in this case might be used by the script to generate
the map of a video game. The data doesn't have to be used that way,
though; maybe the map data is actually embedded in other parts of
the page's markup, and the data block here is just used by the
site's search engine to help users who are looking for particular
features in their game maps.</p>
</div><div class="example">
<p>The following sample shows how a script element can be used to
define a function that is then used by other parts of the
document. It also shows how a <code><a href="#the-script-element">script</a></code> element can be
used to invoke script while the document is being parsed, in this
case to initialize the form's output.</p>
<pre>&lt;script&gt;
function calculate(form) {
var price = 52000;
if (form.elements.brakes.checked)
price += 1000;
if (form.elements.radio.checked)
price += 2500;
if (form.elements.turbo.checked)
price += 5000;
if (form.elements.sticker.checked)
price += 250;
form.elements.result.value = price;
}
&lt;/script&gt;
&lt;form name="pricecalc" onsubmit="return false" onchange="calculate(this)"&gt;
&lt;fieldset&gt;
&lt;legend&gt;Work out the price of your car&lt;/legend&gt;
&lt;p&gt;Base cost: &#163;52000.&lt;/p&gt;
&lt;p&gt;Select additional options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;label&gt;&lt;input type=checkbox name=brakes&gt; Ceramic brakes (&#163;1000)&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;label&gt;&lt;input type=checkbox name=radio&gt; Satellite radio (&#163;2500)&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;label&gt;&lt;input type=checkbox name=turbo&gt; Turbo charger (&#163;5000)&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;label&gt;&lt;input type=checkbox name=sticker&gt; "XZ" sticker (&#163;250)&lt;/label&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Total: &#163;&lt;output name=result&gt;&lt;/output&gt;&lt;/p&gt;
&lt;/fieldset&gt;
&lt;script&gt;
calculate(document.forms.pricecalc);
&lt;/script&gt;
&lt;/form&gt;</pre>
</div><h5 id="scriptingLanguages"><span class="secno">4.3.1.1 </span>Scripting languages</h5><div class="impl">
<p>A user agent is said to <dfn id="support-the-scripting-language">support the scripting language</dfn>
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
case-insensitive</a> match for the <a href="infrastructure.html#mime-type">MIME type</a> string
of a scripting language that the user agent implements.</p>
</div><p>The following lists some <a href="infrastructure.html#mime-type">MIME type</a> strings and the
languages to which they refer:</p><dl><dt>"<code>application/ecmascript</code>"</dt>
<dt>"<code>application/javascript</code>"</dt>
<dt>"<code>application/x-ecmascript</code>"</dt>
<dt>"<code>application/x-javascript</code>"</dt>
<dt>"<code>text/ecmascript</code>"</dt>
<dt>"<code>text/javascript</code>"</dt>
<dt>"<code>text/javascript1.0</code>"</dt>
<dt>"<code>text/javascript1.1</code>"</dt>
<dt>"<code>text/javascript1.2</code>"</dt>
<dt>"<code>text/javascript1.3</code>"</dt>
<dt>"<code>text/javascript1.4</code>"</dt>
<dt>"<code>text/javascript1.5</code>"</dt>
<dt>"<code>text/jscript</code>"</dt>
<dt>"<code>text/livescript</code>"</dt>
<dt>"<code>text/x-ecmascript</code>"</dt>
<dt>"<code>text/x-javascript</code>"</dt>
<dd>JavaScript. <a href="references.html#refsECMA262">[ECMA262]</a></dd>
<dt>"<code>text/javascript;e4x=1</code>"</dt>
<dd>JavaScript with ECMAScript for XML. <a href="references.html#refsECMA357">[ECMA357]</a></dd>
</dl><div class="impl">
<p>User agents may support other <a href="infrastructure.html#mime-type" title="MIME type">MIME
types</a> and other languages.</p>
<p>When examining types to determine if they support the language,
user agents must not ignore unknown MIME parameters &#8212; types
with unknown parameters must be assumed to be unsupported. The <code title="">charset</code> parameter must be treated as an unknown
parameter for the purpose of comparing <a href="infrastructure.html#mime-type" title="MIME type">MIME
types</a> here.</p>
</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="#the-script-element">script</a></code> element
must match the <code title="">script</code> production in the
following ABNF, the character set for which is Unicode. <a href="references.html#refsABNF">[ABNF]</a></p><pre>script = data1 *( escape [ script-start data3 ] "--&gt;" data1 ) [ escape ]
escape = "&lt;!--" data2 *( script-start data3 script-end data2 )
data1 = &lt;any string that doesn't contain a substring that matches not-data1&gt;
not-data1 = "&lt;!--"
data2 = &lt;any string that doesn't contain a substring that matches not-data2&gt;
not-data2 = script-start / "--&gt;"
data3 = &lt;any string that doesn't contain a substring that matches not-data3&gt;
not-data3 = script-end / "--&gt;"
script-start = lt s c r i p t tag-end
script-end = lt slash s c r i p t tag-end
lt = %x003C ; U+003C LESS-THAN SIGN character (&lt;)
slash = %x002F ; U+002F SOLIDUS character (/)
s = %x0053 ; U+0053 LATIN CAPITAL LETTER S
s =/ %x0073 ; U+0073 LATIN SMALL LETTER S
c = %x0043 ; U+0043 LATIN CAPITAL LETTER C
c =/ %x0063 ; U+0063 LATIN SMALL LETTER C
r = %x0052 ; U+0052 LATIN CAPITAL LETTER R
r =/ %x0072 ; U+0072 LATIN SMALL LETTER R
i = %x0049 ; U+0049 LATIN CAPITAL LETTER I
i =/ %x0069 ; U+0069 LATIN SMALL LETTER I
p = %x0050 ; U+0050 LATIN CAPITAL LETTER P
p =/ %x0070 ; U+0070 LATIN SMALL LETTER P
t = %x0054 ; U+0054 LATIN CAPITAL LETTER T
t =/ %x0074 ; U+0074 LATIN SMALL LETTER T
tag-end = %x0009 ; U+0009 CHARACTER TABULATION (tab)
tag-end =/ %x000A ; U+000A LINE FEED (LF)
tag-end =/ %x000C ; U+000C FORM FEED (FF)
tag-end =/ %x0020 ; U+0020 SPACE
tag-end =/ %x002F ; U+002F SOLIDUS (/)
tag-end =/ %x003E ; U+003E GREATER-THAN SIGN (&gt;)</pre><p>When a <code><a href="#the-script-element">script</a></code> element contains <a href="#inline-documentation-for-external-scripts">script
documentation</a>, there are further restrictions on the contents
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="#the-script-element">script</a></code> element's <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute is specified, then the
contents of the <code><a href="#the-script-element">script</a></code> element, if any, must be such
that the value of the <code title="dom-script-text"><a href="#dom-script-text">text</a></code> IDL
attribute, which is derived from the element's contents, matches the
<code title="">documentation</code> production in the following
ABNF, the character set for which is Unicode. <a href="references.html#refsABNF">[ABNF]</a></p><pre>documentation = *( *( space / tab / comment ) [ line-comment ] newline )
comment = slash star *( not-star / star not-slash ) 1*star slash
line-comment = slash slash *not-newline
; characters
tab = %x0009 ; U+0009 CHARACTER TABULATION (tab)
newline = %x000A ; U+000A LINE FEED (LF)
space = %x0020 ; U+0020 SPACE
star = %x002A ; U+002A ASTERISK (*)
slash = %x002F ; U+002F SOLIDUS (/)
not-newline = %x0000-0009 / %x000B-10FFFF
; a Unicode character other than U+000A LINE FEED (LF)
not-star = %x0000-0029 / %x002B-10FFFF
; a Unicode character other than U+002A ASTERISK (*)
not-slash = %x0000-002E / %x0030-10FFFF
; a Unicode character other than U+002F SOLIDUS (/)</pre><p class="note">This corresponds to putting the contents of the
element in JavaScript comments.</p><p class="note">This requirement is in addition to the earlier
restrictions on the syntax of contents of <code><a href="#the-script-element">script</a></code>
elements.</p><div class="example">
<p>This allows authors to include documentation, such as license
information or API information, inside their documents while still
referring to external script files. The syntax is constrained so
that authors don't accidentally include what looks like valid
script while also providing a <code title="attr-script-src"><a href="#attr-script-src">src</a></code> attribute.</p>
<pre>&lt;script src="cool-effects.js"&gt;
// create new instances using:
// var e = new Effect();
// start the effect using .play, stop using .stop:
// e.play();
// e.stop();
&lt;/script&gt;</pre>
</div><div class="impl">
<h5 id="scriptTagXSLT"><span class="secno">4.3.1.4 </span>Interaction of <code><a href="#the-script-element">script</a></code> elements and XSLT</h5>
<p><i>This section is non-normative.</i></p>
<p>This specification does not define how XSLT interacts with the
<code><a href="#the-script-element">script</a></code> element (or, indeed, how XSLT processing
triggers the <a href="the-end.html#stop-parsing">stop parsing</a> steps, how it interacts with
the <a href="history.html#navigate" title="navigate">navigation</a> algorithm, or how it
fits in with the <a href="webappapis.html#event-loop">event loop</a>). However, in the absence
of another specification actually defining this, here are some
guidelines for implementors, based on existing implementations:</p>
<ul><li><p>When an XSLT transformation program is triggered by an <code title="">&lt;?xml-stylesheet?&gt;</code> processing instruction and
the browser implements a direct-to-DOM transformation,
<code><a href="#the-script-element">script</a></code> elements created by the XSLT processor need to
be marked <a href="#parser-inserted">"parser-inserted"</a> and run in document order
(modulo scripts marked <code title="attr-script-defer"><a href="#attr-script-defer">defer</a></code>
or <code title="attr-script-async"><a href="#attr-script-async">async</a></code>), asynchronously
while the transformation is occurring.</p></li>
<li><p>The <code title="dom-XSLTProcessor-transformToDocument">XSLTProcessor.transformToDocument()</code>
method adds elements to a <code><a href="infrastructure.html#document">Document</a></code> that is not in a
<a href="browsers.html#browsing-context">browsing context</a>, and, accordingly, any
<code><a href="#the-script-element">script</a></code> elements they create need to have their
<a href="#already-started">"already started"</a> flag set in the <a href="#prepare-a-script">prepare a
script</a> algorithm and never get executed (<a href="webappapis.html#concept-bc-noscript" title="concept-bc-noscript">scripting is disabled</a>). Such
<code><a href="#the-script-element">script</a></code> elements still need to be marked
<a href="#parser-inserted">"parser-inserted"</a>, though, such that their <code title="dom-script-async"><a href="#dom-script-async">async</a></code> IDL attribute will return
false in the absence of an <code title="attr-script-async"><a href="#attr-script-async">async</a></code> content attribute.</p></li>
<li><p>The <code title="dom-XSLTProcessor-transformToFragment">XSLTProcessor.transformToFragment()</code>
method needs to create a fragment that is equivalent to one built
manually by creating the elements using <code title="dom-document-createElementNS"><a href="infrastructure.html#dom-document-createelementns">document.createElementNS()</a></code>.
For instance, it needs to create <code><a href="#the-script-element">script</a></code> elements that
aren't <a href="#parser-inserted">"parser-inserted"</a> and that don't have their
<a href="#already-started">"already started"</a> flag set, so that they will execute
when the fragment is inserted into a document.</p></li>
</ul><p>The main distinction between the first two cases and the last
case is that the first two operate on <code><a href="infrastructure.html#document">Document</a></code>s and the
last operates on a fragment.</p>
</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>
<dd><a href="content-models.html#metadata-content">Metadata content</a>.</dd>
<dd><a href="content-models.html#flow-content">Flow content</a>.</dd>
<dd><a href="content-models.html#phrasing-content">Phrasing content</a>.</dd>
<dt>Contexts in which this element can be used:</dt>
<dd>In a <code><a href="semantics.html#the-head-element">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>
<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>
<dt>Content model:</dt>
<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">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#the-meta-element">meta</a></code> elements.</dd>
<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">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>
<dd>Otherwise: text that conforms to the requirements given in the prose.</dd>
<dt>Content attributes:</dt>
<dd><a href="elements.html#global-attributes">Global attributes</a></dd>
<dt>DOM interface:</dt>
<dd>Uses <code><a href="elements.html#htmlelement">HTMLElement</a></code>.</dd>
</dl><p>The <code><a href="#the-noscript-element">noscript</a></code> element <a href="rendering.html#represents">represents</a> nothing
if <a href="webappapis.html#concept-n-script" title="concept-n-script">scripting is enabled</a>, and
<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
to present different markup to user agents that support scripting
and those that don't support scripting, by affecting how the
document is parsed.</p><p>When used in <a href="dom.html#html-documents">HTML documents</a>, the allowed content
model is as follows:</p><dl><dt>In a <code><a href="semantics.html#the-head-element">head</a></code> element, if <a href="webappapis.html#concept-n-noscript" title="concept-n-noscript">scripting is disabled</a> for the
<code><a href="#the-noscript-element">noscript</a></code> element</dt>
<dd><p>The <code><a href="#the-noscript-element">noscript</a></code> element must contain only
<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#the-meta-element">meta</a></code>
elements.</p></dd>
<dt>In a <code><a href="semantics.html#the-head-element">head</a></code> element, if <a href="webappapis.html#concept-n-script" title="concept-n-script">scripting is enabled</a> for the
<code><a href="#the-noscript-element">noscript</a></code> element</dt>
<dd><p>The <code><a href="#the-noscript-element">noscript</a></code> element must contain only text,
except that invoking the <a href="the-end.html#html-fragment-parsing-algorithm">HTML fragment parsing
algorithm</a> with
the <code><a href="#the-noscript-element">noscript</a></code> element as the <var title="concept-frag-parse-context"><a href="the-end.html#concept-frag-parse-context">context</a></var> element and the
text contents as the <var title="">input</var> must result in a
list of nodes that consists 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 <code><a href="semantics.html#the-meta-element">meta</a></code> elements that would be
conforming if they were 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>
<dt>Outside of <code><a href="semantics.html#the-head-element">head</a></code> elements, if <a href="webappapis.html#concept-n-noscript" title="concept-n-noscript">scripting is disabled</a> for the
<code><a href="#the-noscript-element">noscript</a></code> element</dt>
<dd><p>The <code><a href="#the-noscript-element">noscript</a></code> element's content model is
<a href="content-models.html#transparent">transparent</a>, with the additional restriction that a
<code><a href="#the-noscript-element">noscript</a></code> element must not have a <code><a href="#the-noscript-element">noscript</a></code>
element as an ancestor (that is, <code><a href="#the-noscript-element">noscript</a></code> can't be
nested).</p></dd>
<dt>Outside of <code><a href="semantics.html#the-head-element">head</a></code> elements, if <a href="webappapis.html#concept-n-script" title="concept-n-script">scripting is enabled</a> for the
<code><a href="#the-noscript-element">noscript</a></code> element</dt>
<dd>
<p>The <code><a href="#the-noscript-element">noscript</a></code> element must contain only text,
except that the text must be such that running the following
algorithm results in a conforming document with no
<code><a href="#the-noscript-element">noscript</a></code> elements and no <code><a href="#the-script-element">script</a></code>
elements, and such that no step in the algorithm causes an
<a href="parsing.html#html-parser">HTML parser</a> to flag a <a href="parsing.html#parse-error">parse error</a>:</p>
<ol><li>Remove every <code><a href="#the-script-element">script</a></code> element from the
document.</li>
<li>Make a list of every <code><a href="#the-noscript-element">noscript</a></code> element in the
document. For every <code><a href="#the-noscript-element">noscript</a></code> element in that list,
perform the following steps:
<ol><li>Let the <var title="">parent element</var> be the parent
element of the <code><a href="#the-noscript-element">noscript</a></code> element.</li>
<li>Take all the children of the <var title="">parent element</var>
that come before the <code><a href="#the-noscript-element">noscript</a></code> element, and call these
elements <var title="">the before children</var>.</li>
<li>Take all the children of the <var title="">parent element</var>
that come <em>after</em> the <code><a href="#the-noscript-element">noscript</a></code> element, and
call these elements <var title="">the after children</var>.</li>
<li>Let <var title="">s</var> be the concatenation of all the
<a href="infrastructure.html#text-node">text node</a> children of the <code><a href="#the-noscript-element">noscript</a></code>
element.</li>
<li>Set the <code title="dom-innerHTML"><a href="apis-in-html-documents.html#dom-innerhtml">innerHTML</a></code>
attribute of the <var title="">parent element</var> to the value
of <var title="">s</var>. (This, as a side-effect, causes the
<code><a href="#the-noscript-element">noscript</a></code> element to be removed from the
document.)</li>
<li>Insert <var title="">the before children</var> at the start of
the <var title="">parent element</var>, preserving their original
relative order.</li>
<li>Insert <var title="">the after children</var> at the end of the
<var title="">parent element</var>, preserving their original
relative order.</li>
</ol></li>
</ol></dd>
</dl><p class="note">All these contortions are required because, for
historical reasons, the <code><a href="#the-noscript-element">noscript</a></code> element is handled
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
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
documents</a>.</p><p class="note"><strong>The <code><a href="#the-noscript-element">noscript</a></code> element is only
effective in <a href="syntax.html#syntax">the HTML syntax</a>, it has no effect in
<a href="the-xhtml-syntax.html#the-xhtml-syntax">the XHTML syntax</a>.</strong></p><div class="impl">
<p>The <code><a href="#the-noscript-element">noscript</a></code> element has no other requirements. In
particular, children of the <code><a href="#the-noscript-element">noscript</a></code> element are not
exempt from <a href="association-of-controls-and-forms.html#form-submission">form submission</a>, scripting, and so forth,
even when <a href="webappapis.html#concept-n-script" title="concept-n-script">scripting is enabled</a>
for the element.</p>
</div><div class="example">
<p>In the following example, a <code><a href="#the-noscript-element">noscript</a></code> element is
used to provide fallback for a script.</p>
<pre>&lt;form action="calcSquare.php"&gt;
&lt;p&gt;
&lt;label for=x&gt;Number&lt;/label&gt;:
&lt;input id="x" name="x" type="number"&gt;
&lt;/p&gt;
&lt;script&gt;
var x = document.getElementById('x');
var output = document.createElement('p');
output.textContent = 'Type a number; it will be squared right then!';
x.form.appendChild(output);
x.form.onsubmit = function () { return false; }
x.oninput = function () {
var v = x.valueAsNumber;
output.textContent = v + ' squared is ' + v * v;
};
&lt;/script&gt;
&lt;noscript&gt;
&lt;input type=submit value="Calculate Square"&gt;
&lt;/noscript&gt;
&lt;/form&gt;</pre>
<p>When script is disabled, a button appears to do the calculation
on the server side. When script is enabled, the value is computed
on-the-fly instead.</p>
<p>The <code><a href="#the-noscript-element">noscript</a></code> element is a blunt
instrument. Sometimes, scripts might be enabled, but for some
reason the page's script might fail. For this reason, it's
generally better to avoid using <code><a href="#the-noscript-element">noscript</a></code>, and to
instead design the script to change the page from being a
scriptless page to a scripted page on the fly, as in the next
example:</p>
<pre>&lt;form action="calcSquare.php"&gt;
&lt;p&gt;
&lt;label for=x&gt;Number&lt;/label&gt;:
&lt;input id="x" name="x" type="number"&gt;
&lt;/p&gt;
<strong>&lt;input id="submit" type=submit value="Calculate Square"&gt;</strong>
&lt;script&gt;
var x = document.getElementById('x');
var output = document.createElement('p');
output.textContent = 'Type a number; it will be squared right then!';
x.form.appendChild(output);
x.form.onsubmit = function () { return false; }
x.oninput = function () {
var v = x.valueAsNumber;
output.textContent = v + ' squared is ' + v * v;
};
<strong> var submit = document.getElementById('submit');
submit.parentNode.removeChild(submit);</strong>
&lt;/script&gt;
&lt;/form&gt;</pre>
<p>The above technique is also useful in XHTML, since
<code><a href="#the-noscript-element">noscript</a></code> is not supported in <a href="the-xhtml-syntax.html#the-xhtml-syntax">the XHTML
syntax</a>.</p>
</div></body></html>