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

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)

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support 1Edge Full support YesFirefox Full support 1
Notes
Full support 1
Notes
Notes 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.
IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
asyncChrome Full support 1Edge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
crossoriginChrome Full support 30Edge Full support YesFirefox Full support 13IE No support NoOpera Full support 12Safari Full support Yes
Notes
Full support Yes
Notes
Notes The crossorigin attribute was implemented in WebKit in WebKit bug 81438.
WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 14Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes
deferChrome Full support Yes
Notes
Full support Yes
Notes
Notes Chrome does not defer scripts with the defer attribute when the page is served as XHTML (application/xhtml+xml) - Chromium Issue #611136, Chromium Issue #874749
Edge Full support YesFirefox Full support 3.5
Notes
Full support 3.5
Notes
Notes 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.
IE Full support 10
Notes
Full support 10
Notes
Notes In versions prior to Internet Explorer 10, it implemented defer by a proprietary specification. Since version 10 it conforms to the W3C specification.
Opera No support NoSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android ? Safari iOS Full support YesSamsung Internet Android Full support Yes
integrityChrome Full support 45Edge Partial support 17Firefox Full support 43IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support 45Chrome Android Full support 45Edge Mobile No support NoFirefox Android Full support 43Opera Android ? Safari iOS No support NoSamsung Internet Android Full support 5.0
language
DeprecatedNon-standard
Chrome Full support 1Edge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
nomodule
Experimental
Chrome Full support YesEdge No support NoFirefox Full support 60
Full support 60
No support 55 — 60
Disabled
Disabled 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.
IE No support NoOpera No support NoSafari No support NoWebView Android Full support YesChrome Android Full support YesEdge Mobile No support NoFirefox Android Full support 60
Full support 60
No support 55 — 60
Disabled
Disabled 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.
Opera Android ? Safari iOS No support NoSamsung Internet Android Full support Yes
srcChrome Full support 1Edge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
textChrome Full support 1Edge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
typeChrome Full support 1Edge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
type.moduleChrome Full support 61Edge Full support 16Firefox Full support 60
Full support 60
No support 54 — 60
Disabled
Disabled 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.
IE No support NoOpera Full support 48Safari Full support 10.1WebView Android Full support 61Chrome Android Full support 61Edge Mobile Full support 16Firefox Android Full support 60
Full support 60
No support 54 — 60
Disabled
Disabled 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.
Opera Android Full support 48Safari iOS Full support 10.3Samsung Internet Android No support No
type: The version parameter of the type attribute
Non-standard
Chrome No support NoEdge No support NoFirefox No support ? — 59IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support ? — 59Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

相容性註解

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,