Questa traduzione è incompleta. Collabora alla traduzione di questo articolo dall’originale in lingua inglese.
L'elemento HTML <script> è usato per incorporare o or reference uno script eseguibile.
| Categorie di contenuto | Metadata content, Flow content, Contenuto del frasario. |
|---|---|
| Contenuto consentito | Script dinamico come text/javascript. |
| Omissione del tag | Nessuna, sia il tag di apertura che quello di chiusura sono obbligatori. |
| Elementi padre consentiti | Qualsiasi elemento che accetta contenuto metadata, o qualsiasi elemento che accetta contenuto del frasario. |
| Ruoli ARIA permessi | Nessuno |
| Interfaccia DOM | HTMLScriptElement |
Attributi
Questo elemento include gli attributi globali.
asyncHTML5- Imposta questo attributo Booleano per indicare al browser che dovrebbe, se possibile, eseguire gli script in in modo asincrono. Non ha effetto negli script inline (ad esempio, script che non hanno l'attributo src).
- Guarda Browser compatibility per le note sul supporto dei browser. Guarda anche script async per asm.js.
integrity- Contiene metadata inline che un user agent può usare per verifica che una risorsa elaborata è distribuita senza manipolazioni. Guarda Subresource Integrity.
src- Questo attributo specifica l'URI di uno script esterno; può essere usato in alternativa all'incorporamento dello script direttamente all'interno del documento. Se un elemento
scriptha un attrbituosrcspecificato, non deve avere uno script incorporato tra i suoi tag. type- Questo attributo identifica il linguaggio di scripting del codice incorporato all'interno dell'elemento
scripto con referenza tramite l'attributosrc. Questo è specificato come tipo MIME; esempi di tipi MIME includonotext/javascript,text/ecmascript,application/javascript, eapplication/ecmascript. Se questo attributo è assente, lo script è trattato come JavaScript. - Se il tipo MIME non è di tipo JavaScript, il contenuto incorporato all'interno dei suoi tag è trattato come un blocco di data che non verrà processato dal browser.
- Se il tipo specificato è
moduleil codice è trattato come modulo JavaScript . Guarda ES6 in Depth: Modules
Nota che in Firefox puoi usare delle funzionalità avanzate come le dichiarazionileted altre funzionalità delle ultime versioni di JS , usandotype=application/javascript;version=1.8. Attenzione, però, che questa non è una funzionalità standard, probabilmente romperà il supporto ad altri browser, in particolare quelli basati su Chromium. - Per sapere come includere linguaggi di programmazione esoterici, leggi Rosetta.
text- Come l'attributo
textContent, impsota il contenuto testuale dell'elemento. A differenza dell'attributotextContent, però, questo attributo è valutato come codice eseguibile dopo che il nodo viene inserito nel DOM. language- Come l'attributo
type, questo attributo identifica il linguagio di scripting in uso. A differenza dell'attributotype, però, i valori possibili di questo attributo non sono mai stati resi standard. Si deve usare, invece, l'attributotype. defer- Questo attributo booleano è impostanto per indicare al browser che lo script dev'essere eseguito dopo che il documento viene elaborato, ma prima dell'evento
DOMContentLoaded. L'attributodeferdovrebbe essere usato solo su script esterni. crossorigin- Gli elementi
scriptnormali minimal information to thewindow.onerrorfor scripts which do not pass the standard CORS checks. To allow error logging for sites which use a separate domain for static media, use this attribute. See CORS settings attributes for a more descriptive explanation of the valid arguments.
Note
Gli script senza gli attributi async o defer, così come gli script in linea, sono presi ed eseguito immediatamente, prima che il browser continui ad elaborare la pagina.
Gli script dovrebbero essere serviti con il tipo MIME text/javascript, ma i browser sono clementi e li bloccano solo se lo script è servito con un tipo immagine (image/*), video (video/*), audio (audio/*) o text/csv. Se lo script è bloccato, un evento error è inviato all'elemento, se no viene inviato un evento success.
Esempi
<!-- HTML4 and (x)HTML --> <script type="text/javascript" src="javascript.js"></script> <!-- HTML5 --> <script src="javascript.js"></script>
Specifiche
| Specifica | Stato | Commenti |
|---|---|---|
| WHATWG HTML Living Standard The definition of '<script>' in that specification. |
Living Standard | Aggiunge il tipo module |
| HTML5 The definition of '<script>' in that specification. |
Recommendation | |
| HTML 4.01 Specification The definition of '<script>' in that specification. |
Recommendation | |
| Subresource Integrity The definition of '<script>' in that specification. |
Recommendation | Aggiunge l'attributo integrity |
Compatibilità con i browser
| Funzionalità | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| Supporto base | 1.0 | (Yes) | 1.0 (1.7 or earlier)[2] | (Yes) | (Yes) | (Yes) |
|
attributo async |
(Yes)[1] | (Yes) | 3.6 (1.9.2)[1] | 10[1] | 15[1] | (Yes)[1] |
| attributo defer | (Yes) | (Yes) | 3.5 (1.9.1)[6] |
4[3] |
No support | (Yes) |
| attributo crossorigin | 30.0 | ? | 13 (13) | No support | 12.50 | (Yes)[4] |
| attributo integrity | 45.0 | ? | 43 (43) | No support[5] |
| Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|---|
| Supporto base | (Yes) | (Yes) | (Yes) | 1.0 (1.0)[2] | (Yes) | (Yes) | (Yes) | (Yes) |
| attributo async | (Yes)[1] | (Yes)[1] | ? | 1.0 (1.0)[1] | No support[1] | ?[1] | (Yes)[1] | (Yes)[1] |
| attributo defer | (Yes) | (Yes) | ? | 1.0 (1.0) | No support | ? | (Yes) | (Yes) |
| attributo integrity | No support | 45.0 | ? | 43 (43) | 45.0 |
[1] In older browsers that don't support the async attribute, parser-inserted scripts block the parser; script-inserted scripts execute asynchronously in IE and WebKit, but synchronously in Opera and pre-4.0 Firefox. In Firefox 4.0, the async DOM property defaults to true for script-created scripts, so the default behavior matches the behavior of IE and WebKit. To request script-inserted external scripts be executed in the insertion order in browsers where the document.createElement("script").async evaluates to true (such as Firefox 4.0), set .async=false on the scripts you want to maintain order. Never call document.write() from an async script. In Gecko 1.9.2, calling document.write() has an unpredictable effect. In Gecko 2.0, calling document.write() from an async script has no effect (other than printing a warning to the error console).
[2] Starting in Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1), inserting script elements that have been created by calling document.createElement("script") into the DOM no longer enforces execution in insertion order. This change lets Gecko properly abide by the HTML5 specification. To make script-inserted external scripts execute in their insertion order, set .async=false on them.
Also, <script> elements inside <iframe>, <noembed> and <noframes> elements are now executed, for the same reasons.
[3] In versions prior to Internet Explorer 10 Trident implemented <script> by a proprietary specification. Since version 10 it conforms to the W3C specification.
[4] The crossorigin attribute was implemented in WebKit in WebKit bug 81438.
[5] WebKit bug 148363 tracks WebKit implementation of Subresource Integrity (which includes the integrity attribute).
[6] Since Gecko 1.9.2 (Firefox 3.6 / Thunderbird 3.1 / Fennec 1.0), the defer attribute is ignored on scripts that don't have the src attribute. However, in Gecko 1.9.1 (Firefox 3.5 / Thunderbird 3.0 / SeaMonkey 2.0) even inline scripts are deferred if the defer attribute is set.