翻譯不完整。請協助 翻譯此英文文件

HTML <script> 元素能嵌入或引用要執行的程式碼。最常見的用途是嵌入或引用 JavaScript 程式碼。<script> 元素也能執行其他語言,例如 WebGL 的 GLSL shader 程式語言。

內容類型 元資料內容流型內容Phrasing content.
允許的內容 動態腳本如 text/javascript.
標籤省略 None, both the starting and ending tag are mandatory.
允許的父元素 任何可接受 元資料內容phrasing content 的元素。
允許的 ARIA role None
DOM 介面 HTMLScriptElement

屬性

此元素包含了全域屬性

async HTML5
這個布林屬性告訴瀏覽器說:如果可以,就以非同步的方法執行腳本。

如果沒有 src 屬性的話,就不能用這個屬性(例如行內腳本):在這種情況下,它將失去作用。

在 HTML 解析時,瀏覽器通常會假定最壞的情況,並同步地載入腳本(例如 async=false)。

動態插入的腳本(例如 document.createElement)一般來說是非同步執行的。因此,如果設定同步的話(腳本按照被插入的順序執行),會被設為 async=false

請參見 Browser compatibility 的瀏覽器支援備註。另請參見 Async scripts for asm.js

crossorigin
針對沒有通過標準 CORS 的一般 script 元素,會把最少的資訊傳給 window.onerror。若要允許另一個域名站點的靜態內容,列出錯誤訊息,請使用此屬性。請參見 CORS settings attributes 以以取得對其有效參數的,更具描述性的解釋。
defer
This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed, but before firing DOMContentLoaded.
Scripts with the defer attribute will prevent the DOMContentLoaded event from firing until the script has loaded and finished evaluating.

如果沒有 src 屬性的話,就不能用這個屬性(例如行內腳本):在這種情況下,它將失去作用。

To achieve a similar effect for dynamically inserted scripts use async=false instead. Scripts with the defer attribute will execute in the order in which they appear in the document.
integrity
This attribute contains inline metadata that a user agent can use to verify that a fetched resource has been delivered free of unexpected manipulation. See Subresource Integrity.
nomodule
這個布林屬性,會要求支援 ES2015 modules 的瀏覽器,不執行裡面的程式。這能用來給不支援 JavaScript 模組的老舊瀏覽器,提供用於向下支援的服務。
nonce
A cryptographic nonce (number used once) to whitelist inline scripts in a script-src Content-Security-Policy. The server must generate a unique nonce value each time it transmits a policy. It is critical to provide a nonce that cannot be guessed as bypassing a resource's policy is otherwise trivial.
src
此屬性指定外部程式的 URI,可以用來取代直接在文件內中嵌入腳本。

如果 script 元素有指定 src 屬性的話,標籤內就不能有嵌入的腳本。

text
Like the textContent attribute, this attribute sets the text content of the element. Unlike the textContent attribute, however, this attribute is evaluated as executable code after the node is inserted into the DOM.
type

此屬性指定程式碼應該表示的類型。此屬性的值會屬於以下類別之一:

  • 省略或 JavaScript MIME 類型:針對相容 HTML5 的瀏覽器來說,元素內會執行 JavaScript。HTML5 規範敦促作者省略此屬性,不需要寫冗長的 MIME 類型。在早期的瀏覽器中,這確定了嵌入或引入(透過 src 屬性)腳本的語言。JavaScript MIME 類型有列在規範內
  • moduleHTML5 針對相容 HTML5 的瀏覽器來說,這段程式碼會當成 JavaScript 模組(module)。腳本內容的處理不受 charsetdefer 屬性影響。針對 module 的資訊,請參閱 ES6 in Depth: Modules。在使用 module 關鍵字時,程式碼的行為會有所不同。
  • 其他值:嵌入的內容會被當成一段不給瀏覽器執行的資料塊(data block)。開發者應當使用非 JavaScript 的有效 MIME 類型,以標明資料塊。src 屬性也將被忽略。

注:在 Firefox 你可以在 <script> 元素的 type 屬性,透過標明非標準參數 version,指定要使用的 JavaScript 版本:例如說 type="application/javascript;version=1.8"。這個功能在 Firefox 59 移除了(請參見 bug 1428745)。

棄用屬性

language
如同 type 屬性,此屬性指定正在使用的腳本語言。但與 type 屬性不同,此屬性的可用值從未標準化。應當使用 type 屬性。

註解

Scripts without async or defer attributes, as well as inline scripts, are fetched and executed immediately, before the browser continues to parse the page.

The script should be served with the text/javascript MIME type, but browsers are lenient and only block them if the script is served with an image type (image/*); a video type (video/*); an audio (audio/*) type; or text/csv. If the script is blocked, an error is sent to the element, if not a load event is sent.

示例

基本

以下示例展示如何在 HTML4 與 HTML5 使用 <script> 屬性。

<!-- HTML4 與  (x)HTML -->
<script type="text/javascript" src="javascript.js"></script>

<!-- HTML5 -->
<script src="javascript.js"></script>

模組的向下支援方案

有針對 type 屬性支援 module(模組)的瀏覽器,會忽略 nomodule 屬性的程式碼。這能讓那些不支援模組的瀏覽器,提供替代的使用方法。

<script type="module" src="main.mjs"></script>
<script nomodule src="fallback.js"></script>

規範

規範 狀態 註解
HTML Living Standard
The definition of '<script>' in that specification.
Living Standard Adds the module type
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 Adds the integrity attribute.

瀏覽器相容性

The information shown below has been pulled from MDN's Github (https://github.com/mdn/browser-compat-data)

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support1 Yes11 Yes Yes Yes
async1 Yes1 Yes Yes Yes
crossorigin30 Yes13 No12 Yes2
defer Yes Yes3.53104 No Yes
integrity45 No43 No ? No5
language1 Yes1 Yes Yes Yes
nomodule Yes No

60

55 — 606

No No No
src1 Yes1 Yes Yes Yes
text1 Yes1 Yes Yes Yes
type1 Yes1 Yes Yes Yes
type.module6116

60

54 — 607

No4810.1
type: The version parameter of the type attribute No No Yes — 59 No No No
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes4 Yes Yes Yes
async Yes Yes Yes4 Yes Yes Yes
crossorigin Yes Yes ?14 ? ? Yes
defer Yes Yes Yes4 ? Yes Yes
integrity4545 No43 ? No5.0
language Yes Yes Yes4 Yes Yes Yes
nomodule Yes Yes No

60

55 — 606

? No Yes
src Yes Yes Yes4 Yes Yes Yes
text Yes Yes Yes4 Yes Yes Yes
type Yes Yes Yes4 Yes Yes Yes
type.module616116

60

54 — 607

4810.3 No
type: The version parameter of the type attribute No No No Yes — 59 No No No

1. Starting in Firefox 4, inserting <script> elements that have been created by calling document.createElement("script") no longer enforces execution in insertion order. This change lets Firefox properly abide by the specification. To make script-inserted external scripts execute in their insertion order, set .async=false on them.

2. The crossorigin attribute was implemented in WebKit in WebKit bug 81438.

3. Since Firefox 3.6, the defer attribute is ignored on scripts that don't have the src attribute. However, in Firefox 3.5 even inline scripts are deferred if the defer attribute is set.

4. In versions prior to Internet Explorer 10, it implemented defer by a proprietary specification. Since version 10 it conforms to the W3C specification.

5. WebKit bug 148363 tracks WebKit implementation of Subresource Integrity (which includes the integrity attribute).

6. From version 55 until version 60 (exclusive): this feature is behind the dom.moduleScripts.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.

7. From version 54 until version 60 (exclusive): this feature is behind the dom.moduleScripts.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.

相容性註解

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 Firefox. In Firefox 4, the async DOM property defaults to true for script-created scripts, so the default behaviour matches the behaviour 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), set .async=false on the scripts you want to maintain order.

Never call document.write() from an async script. In Firefox 3.6, calling document.write() has an unpredictable effect. In Firefox 4, calling document.write() from an async script has no effect (other than printing a warning to the error console).

參見

文件標籤與貢獻者

此頁面的貢獻者: SphinxKnight, t7yang, iigmir, royxnatw, Shiyou
最近更新: SphinxKnight,