Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

<script>

概述

HTML <script> 元素的作用是在HTML或XHTML文档中嵌入或引用可执行的脚本没有asyncdefer属性的脚本和内联脚本会在浏览器继续解析剩余文档前被获取并立刻执行

属性

该元素包含全局属性

async HTML5
该布尔属性指示浏览器是否在允许的情况下异步执行该脚本。该属性对于内联脚本无作用 (即没有src属性的脚本)。
关于浏览器支持请参见浏览器兼容性。另可参见文章asm.js的异步脚本.
src
这个属性定义引用外部脚本的URI,这可以用来代替直接在文档中嵌入脚本。有src属性的script元素标签内不应该再有嵌入的脚本
type
该属性定义script元素包含或src引用的脚本语言。属性的值为MIME类型; 支持的MIME类型包括text/javascript, text/ecmascript, application/javascript, 和application/ecmascript。如果没有定义这个属性,脚本会被视作JavaScript。
如果MIME类型不是JavaScript类型(上述支持的类型),则该元素所包含的内容会被当作数据块而不会被浏览器执行。
如果type属性为module,代码会被当作JavaScript模块 。请参见ES6 in Depth: Modules
在Firefox中可以通过定义type=application/javascript;version=1.8来使用如let声明这类的JS高版本中的先进特性。 但请注意这是个非标准功能,其他浏览器,特别是基于Chrome的浏览器可能会不支持。
关于如何引入特殊编程语言,请参见这篇文章
language
type属性类似,这个属性定义脚本使用的语言。 但是与type不同的是,这个属性的可能值从未被标准化过。请用type属性代替这个属性。
defer
这个布尔属性定义该脚本是否会延迟到文档解析完毕后才执行。但因为这个功能还未被所有主流浏览器实施,开发人员不应假设脚本实际上都会被延迟执行。defer属性不应在没有src属性的脚本标签上使用。从Gecko 1.9.2开始, 没有src属性的脚本标签的defer属性会被忽略。但是在Gecko 1.9.1中,如果定义了defer属性,即使内嵌的脚本也会被延迟执行。
crossorigin
Normal script tags will pass minimal information to the window.onerror for scripts which do not pass the standard CORS checks. To allow error logging for sites which use a separate domain for static media, several browsers have enabled the crossorigin attribute for scripts using the same definition as the standard img crossorigin attribute. Efforts to standardize this attribute are underway on the WHATWG mailing list.

示例

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

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

规范

规范 状态 备注
HTML5
<script>
Recommendation  
HTML 4.01 Specification
<script>
Recommendation  

浏览器兼容性

功能 Chrome Firefox (Gecko) Internet Explorer Opera Safari
基本支持 1.0 1.0 (1.7 or earlier) (Yes) (Yes) (Yes)
async属性 (Yes) 3.6 (1.9.2) 10 未实现 (Yes)
defer属性 (Yes) 3.5 (1.9.1)

4 (非标准实现)

10 (标准实现)

未实现 (Yes)
crossorigin 属性 30.0 Chromium Bug 159566 13 (13) bug 696301 未实现 12.50 (Yes)

 (WebKit bug 81438)

功能 Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
基本支持 (Yes) 1.0 (1.0) (Yes) (Yes) (Yes)
async 属性 (Yes) 1.0 (1.0) 未实现 ? (Yes)
defer属性 (Yes) 1.0 (1.0) 未实现 ? (Yes)

异步支持

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).

Gecko特别备注

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.

 

参见

文档标签和贡献者

标签: 
 此页面的贡献者: ahwassa, gqqnbig, fskuok
 最后编辑者: ahwassa,