ディレクトリ 検索
Attributes accesskey (attribute) class (attribute) contenteditable (attribute) contextmenu (attribute) data-* (attribute) dir (attribute) draggable (attribute) dropzone (attribute) Global attributes hidden (attribute) id (attribute) itemid (attribute) itemprop (attribute) itemref (attribute) itemscope (attribute) itemtype (attribute) lang (attribute) slot (attribute) spellcheck (attribute) style (attribute) tabindex (attribute) title (attribute) translate (attribute) Elements a abbr address area article aside audio b base bdi bdo blockquote body br button canvas caption cite code col colgroup data datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 head header hr html i iframe img input input type="button" input type="checkbox" input type="color" input type="date" input type="datetime"-local input type="email" input type="file" input type="hidden" input type="image" input type="month" input type="number" input type="password" input type="radio" input type="range" input type="reset" input type="search" input type="submit" input type="tel" input type="text" input type="time" input type="url" input type="week" ins kbd label legend li link main map mark menu menuitem meta meter nav noscript object ol optgroup option output p param picture pre progress q rp rt rtc ruby s samp script section select slot small source span strong style sub summary sup table tbody td template textarea tfoot th thead time title tr track u ul var video wbr Miscellaneous Attributes Block-level elements CORS enabled image CORS settings attributes Element Inline elements Kinds of HTML content Link types Microdata Optimizing your pages for speculative parsing Preloading content Reference Supported media formats Using the application cache Obsolete acronym applet basefont big blink center command content dir element font frame frameset hgroup image input type="datetime" isindex keygen listing marquee nextid noframes plaintext strike tt xmp
テキスト

HTML<script> 元素用于嵌入或引用可执行脚本。

内容类别


Metadata content, Flow content, Phrasing content.

允许的内容

动态脚本,如文本/ JavaScript。

标记遗漏

没有,起始和结束标签都是强制性的。

允许父母

任何接受  metadata content 元素,或任何接受  phrasing content 的元素。

允许ARIA角色

没有

DOM界面

HTMLScriptElement

属性

这个元素包含全局属性。

asyncHTML5该布尔属性指示浏览器是否在允许的情况下异步执行该脚本。该属性对于内联脚本无作用 (即没有src属性的脚本)。

动态插入的脚本默认情况下是异步执行的,所以打开同步执行(即脚本以加载的顺序执行)设置async=false

请参阅浏览器兼容性以获取关于浏览器支持的注意事项。另请参阅asm.js的异步脚本。

crossorigin普通的script元素将最小的信息传递给window.onerror没有通过标准CORS检查的脚本。要允许对使用静态媒体的单独域的站点进行错误日志记录,请使用此属性。有关有效参数的更多描述性说明,请参阅CORS设置属性。

defer这个布尔属性被设置为向浏览器指示该脚本是在文件被解析之后,但在发射之前被执行的DOMContentLoaded。如果该src属性不存在(即内联脚本),则不能使用该属性,在这种情况下,该属性不起作用。为了达到类似的动态插入脚本的效果,可以使用async=false

integrity包含内联元数据,用户代理可以使用这些内联元数据来验证提取的资源是否已经交付,没有意外的操作。请参阅子资源完整性。

nomodule此布尔属性被设置为指示脚本不应该在支持ES6模块的浏览器中执行- 实际上,这可以用于将回退脚本提供给不支持模块化JavaScript代码的旧浏览器。

src此属性指定外部脚本的URI; 这可以用来替代直接在文档中嵌入脚本。如果一个script元素有一个src指定的属性,它不应该在其标签内嵌入一个脚本。

texttextContent 属性一样,这个属性设置元素的文本内容。textContent 然而,与属性不同的是,在将节点插入DOM之后,该属性被评估为可执行代码。

type

指示表示的脚本的类型。此属性的值将在以下类别之一中:

  • 省略或JavaScript MIME类型:对于符合HTML5的浏览器,这表示脚本是JavaScript。HTML5规范敦促作者省略该属性,而不是提供冗余的MIME类型。在早期的浏览器中,这标识了嵌入或导入(通过src属性)代码的脚本语言。规范中列出了 JavaScript MIME类型。

  • module**:** HTML5对于符合HTML5的浏览器,代码被视为JavaScript模块。脚本内容的处理不受charsetdefer属性的影响。有关使用的信息module,请参见深度:模块中的ES6。

  • 任何其他值或MIME类型:嵌入式内容被视为不会被浏览器处理的数据块。该src属性将被忽略。

请注意,在Firefox中,您可以使用高级功能,例如稍后JS版本中的let语句和其他功能type=application/javascript;version=1.8。但是,要小心,因为这是一个非标准的功能,这很可能会打破对其他浏览器的支持,特别是基于Chromium的浏览器。

关于如何包含异国情调的编程语言,请阅读关于Rosetta。

已弃用的属性

languagetype属性一样,此属性标识正在使用的脚本语言。type然而,与属性不同的是,这个属性的可能值从未标准化。type应该使用该属性来代替。

注意

浏览器继续解析页面之前,立即获取并执行没有asyncdefer属性的脚本以及内联脚本。

该脚本应该与text/javascriptMIME类型一起提供,但是浏览器是宽松的,只有在脚本使用图像类型(image/*),视频类型(video/*),音频(audio/*)类型或者text/csv。如果脚本被阻塞,error则发送给元素,否则load发送事件。

例子

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

规范

Specification

Status

Comments

HTML Living StandardThe definition of '<script>' in that specification.

Living Standard

Adds the module type

HTML5The definition of '<script>' in that specification.

Recommendation


HTML 4.01 SpecificationThe definition of '<script>' in that specification.

Recommendation


Subresource IntegrityThe definition of '<script>' in that specification.

Recommendation

Adds the integrity attribute.

浏览器兼容性

Feature

Chrome

Edge

Firefox

Internet Explorer

Opera

Safari

Basic Support

1

(Yes)

11

(Yes)

(Yes)

(Yes)

async

1

(Yes)

1

(Yes)

(Yes)

(Yes)

crossorigin

30

(Yes)

13

No

12.5

(Yes)2

defer

(Yes)

(Yes)

3.53

104

No

(Yes)

integrity

45

No

43

No

?

No5

language

1

(Yes)

1

(Yes)

(Yes)

(Yes)

module

No

No

No

No

No

(Yes)

nomodule

(Yes)

No

(Yes)6

No

No

No

src

1

(Yes)

1

(Yes)

(Yes)

(Yes)

text

1

(Yes)

1

(Yes)

(Yes)

(Yes)

type

1

(Yes)

1

(Yes)

(Yes)

(Yes)

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

(Yes)

(Yes)

(Yes)

11

(Yes)

(Yes)

(Yes)

async

(Yes)

(Yes)

(Yes)

1

(Yes)

(Yes)

(Yes)

crossorigin

(Yes)

(Yes)

?

13

No

?

?

defer

(Yes)

(Yes)

(Yes)

1.0

No

?

(Yes)

integrity

45

45

No

43

No

?

No

language

(Yes)

(Yes)

(Yes)

1

(Yes)

(Yes)

(Yes)

module

No

No

No

No

No

No

(Yes)

nomodule

(Yes)

(Yes)

No

(Yes)6

No

?

No

src

(Yes)

(Yes)

(Yes)

1

(Yes)

(Yes)

(Yes)

text

(Yes)

(Yes)

(Yes)

1

(Yes)

(Yes)

(Yes)

type

(Yes)

(Yes)

(Yes)

1

(Yes)

(Yes)

(Yes)

前の記事: 次の記事: