Annotation of html5/spec/scripting-1.html, revision 1.53

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

Webmaster