1.仕様
SCRIPT要素
は、
実行できるコードを
埋め込んだり
参照したりできます。
- script
-
スクリプト (Script statements)
- 種類
-
生テキスト要素
テキストを持つことができる要素です。
- 使用できるコンテキスト
-
- メタデータ
コンテンツ を受け入れる要素 - フレージング
コンテンツ を受け入れる要素 - スクリプトを
サポートする要素 を受け入れる要素
メタデータ
コンテンツ を受け入れる要素のコンテンツとして、使用できます。
フレージングコンテンツ を受け入れる要素のコンテンツとして、使用できます。
スクリプトをサポートする要素 を受け入れる要素のコンテンツとして、使用できます。メタデータ
コンテンツ を受け入れる要素は、head要素 です。フレージング
コンテンツ を受け入れる要素は、h1要素 , h2要素 , h3要素 , h4要素 , h5要素 , h6要素 , p要素 , pre要素 , output要素 , progress要素 , meter要素 , label要素 , datalist要素 , body要素 , section要素 , nav要素 , aside要素 , article要素 , header要素 , footer要素 , main要素 , address要素 , li要素 , dt要素 , dd要素 , caption要素 , th要素 , td要素 , form要素 , fieldset要素 , search要素 , blockquote要素 , figure要素 , figcaption要素 , div要素 , dialog要素 , abbr要素 , b要素 , bdi要素 , bdo要素 , cite要素 , code要素 , data要素 , dfn要素 , em要素 , i要素 , kbd要素 , mark要素 , q要素 , s要素 , samp要素 , small要素 , span要素 , strong要素 , sub要素 , sup要素 , u要素 , var要素 です。スクリプトを
サポートする要素 を受け入れる要素は、hgroup要素 , ol要素 , ul要素 , menu要素 , dl要素 , div要素 , picture要素 , table要素 , tbody要素 , thead要素 , tfoot要素 , tr要素 , select要素 , datalist要素 , optgroup要素 です。 - メタデータ
- コンテンツモデル
-
スクリプトのコンテンツ制限に一致するテキスト。
スクリプトのコンテンツ制限に一致するテキスト。
src属性がない場合は、type属性の値に依存しますが、スクリプトのコンテンツ制限に一致する必要があります。
src属性がある場合は、その要素は空であるか、スクリプトのコンテンツ制限にも一致するスクリプトドキュメントのみを含む必要があります。
- タグの省略
-
どちらのタグも省略できません。
- コンテンツ属性
-
グローバル属性 他
- src
属性 - リソースのアドレス
- type
属性 - スクリプトの種類
- nomodule
属性 - モジュールスクリプトをサポートするユーザーエージェントでの実行を防止します
- async
属性 - フェッチ中にブロックせずに、可能な場合はスクリプトを実行します
- defer
属性 - スクリプトの実行を延期する
- crossorigin
属性 - 要素がクロスオリジンリクエストを処理する方法
- integrity
属性 - サブリソース整合性チェックで使用される整合性メタデータ[SRI]
- referrerpolicy
属性 - 要素によって開始されたフェッチのリファラーポリシー
- blocking
属性 - 要素が潜在的にレンダリングをブロックしているかどうか
- fetchpriority
属性 - 要素によって開始されるフェッチの優先順位を設定します。
charset属性がありましたが、廃止されました。作成者はscript要素にcharset属性を指定すべきではありません。属性が存在する場合、その値は 'utf-8' の大文字小文字を区別しないASCIIマッチでなければなりません。(これは、UTF-8としてエンコードされるという要件に準拠している文書では効果がありません)。属性を省略します。ドキュメントもスクリプトもUTF-8を使うことが要求されており、スクリプト要素はドキュメントを継承しているので、スクリプト要素で指定するのは冗長です。
language属性がありましたが、廃止されました。作成者はscript要素にlanguage属性を指定すべきではありません。属性が存在する場合、その値は文字列 'JavaScript' のASCII大文字小文字の区別のないマッチでなければならず、type属性は省略されるか、その値は文字列 'text/javascript' のASCII大文字小文字の区別のないマッチでなければなりません。この属性は、完全に省略されるか ('JavaScript' という値では、何の効果もありません)、type属性で置き換えられるべきです。JavaScriptの場合はこの属性を省略します。データ・ブロックの場合は、代わりにtype属性を使用します。
event属性, for属性がありましたが、廃止されました。DOM イベントメカニズムを使用してイベントリスナーを登録します。
- src
- DOM インターフェイス
-
HTMLScriptElement
[Exposed=Window]
interface HTMLScriptElement : HTMLElement {
[HTMLConstructor] constructor();
[CEReactions] attribute USVString src;
[CEReactions] attribute DOMString type;
[CEReactions] attribute boolean noModule;
[CEReactions] attribute boolean async;
[CEReactions] attribute boolean defer;
[CEReactions] attribute DOMString? crossOrigin;
[CEReactions] attribute DOMString text;
[CEReactions] attribute DOMString integrity;
[CEReactions] attribute DOMString referrerpolicy;
[SameObject, PutForwards=value] readonly attribute DOMTokenList blocking;
[CEReactions] attribute DOMString fetchPriority;
static boolean supports(DOMString type);
// also has obsolete members
};
HTMLElement
[Exposed=Window]
interface HTMLElement : Element {
[HTMLConstructor] constructor();
// metadata attributes
[CEReactions] attribute DOMString title;
[CEReactions] attribute DOMString lang;
[CEReactions] attribute boolean translate;
[CEReactions] attribute DOMString dir;
// user interaction
[CEReactions] attribute (boolean or unrestricted double or DOMString)? hidden;
[CEReactions] attribute DOMString inert;
undefined click();
[CEReactions] attribute DOMString accessKey;
readonly attribute DOMString accessKeyLabel;
[CEReactions] attribute boolean draggable;
[CEReactions] attribute boolean spellcheck;
[CEReactions] attribute DOMString writingSuggestions;
[CEReactions] attribute DOMString autocapitalize;
[CEReactions] attribute [LegacyNullToEmptyString] DOMString innerText;
[CEReactions] attribute [LegacyNullToEmptyString] DOMString outerText;
ElementInternals attachInternals();
// The popover API
undefined showPopover();
undefined hidePopover();
boolean togglePopover(optional boolean force);
[CEReactions] attribute DOMString? popover;
};
HTMLElement includes GlobalEventHandlers;
HTMLElement includes ElementContentEditable;
HTMLElement includes HTMLOrSVGElement;
Element
[Exposed=Window]
interface Element : Node {
readonly attribute DOMString? namespaceURI;
readonly attribute DOMString? prefix;
readonly attribute DOMString localName;
readonly attribute DOMString tagName;
[CEReactions] attribute DOMString id;
[CEReactions] attribute DOMString className;
[SameObject, PutForwards=value] readonly attribute DOMTokenList classList;
[CEReactions, Unscopable] attribute DOMString slot;
boolean hasAttributes();
[SameObject] readonly attribute NamedNodeMap attributes;
sequence<DOMString> getAttributeNames();
DOMString? getAttribute(DOMString qualifiedName);
DOMString? getAttributeNS(DOMString? namespace, DOMString localName);
[CEReactions] undefined setAttribute(DOMString qualifiedName, DOMString value);
[CEReactions] undefined setAttributeNS(DOMString? namespace, DOMString qualifiedName, DOMString value);
[CEReactions] undefined removeAttribute(DOMString qualifiedName);
[CEReactions] undefined removeAttributeNS(DOMString? namespace, DOMString localName);
[CEReactions] boolean toggleAttribute(DOMString qualifiedName, optional boolean force);
boolean hasAttribute(DOMString qualifiedName);
boolean hasAttributeNS(DOMString? namespace, DOMString localName);
Attr? getAttributeNode(DOMString qualifiedName);
Attr? getAttributeNodeNS(DOMString? namespace, DOMString localName);
[CEReactions] Attr? setAttributeNode(Attr attr);
[CEReactions] Attr? setAttributeNodeNS(Attr attr);
[CEReactions] Attr removeAttributeNode(Attr attr);
ShadowRoot attachShadow(ShadowRootInit init);
readonly attribute ShadowRoot? shadowRoot;
Element? closest(DOMString selectors);
boolean matches(DOMString selectors);
boolean webkitMatchesSelector(DOMString selectors); // legacy alias of .matches
HTMLCollection getElementsByTagName(DOMString qualifiedName);
HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);
HTMLCollection getElementsByClassName(DOMString classNames);
[CEReactions] Element? insertAdjacentElement(DOMString where, Element element); // legacy
undefined insertAdjacentText(DOMString where, DOMString data); // legacy
};
GlobalEventHandlers
interface mixin GlobalEventHandlers {
attribute EventHandler onabort;
attribute EventHandler onauxclick;
attribute EventHandler onbeforeinput;
attribute EventHandler onbeforematch;
attribute EventHandler onbeforetoggle;
attribute EventHandler onblur;
attribute EventHandler oncancel;
attribute EventHandler oncanplay;
attribute EventHandler oncanplaythrough;
attribute EventHandler onchange;
attribute EventHandler onclick;
attribute EventHandler onclose;
attribute EventHandler oncontextlost;
attribute EventHandler oncontextmenu;
attribute EventHandler oncontextrestored;
attribute EventHandler oncopy;
attribute EventHandler oncuechange;
attribute EventHandler oncut;
attribute EventHandler ondblclick;
attribute EventHandler ondrag;
attribute EventHandler ondragend;
attribute EventHandler ondragenter;
attribute EventHandler ondragleave;
attribute EventHandler ondragover;
attribute EventHandler ondragstart;
attribute EventHandler ondrop;
attribute EventHandler ondurationchange;
attribute EventHandler onemptied;
attribute EventHandler onended;
attribute OnErrorEventHandler onerror;
attribute EventHandler onfocus;
attribute EventHandler onformdata;
attribute EventHandler oninput;
attribute EventHandler oninvalid;
attribute EventHandler onkeydown;
attribute EventHandler onkeypress;
attribute EventHandler onkeyup;
attribute EventHandler onload;
attribute EventHandler onloadeddata;
attribute EventHandler onloadedmetadata;
attribute EventHandler onloadstart;
attribute EventHandler onmousedown;
[LegacyLenientThis] attribute EventHandler onmouseenter;
[LegacyLenientThis] attribute EventHandler onmouseleave;
attribute EventHandler onmousemove;
attribute EventHandler onmouseout;
attribute EventHandler onmouseover;
attribute EventHandler onmouseup;
attribute EventHandler onpaste;
attribute EventHandler onpause;
attribute EventHandler onplay;
attribute EventHandler onplaying;
attribute EventHandler onprogress;
attribute EventHandler onratechange;
attribute EventHandler onreset;
attribute EventHandler onresize;
attribute EventHandler onscroll;
attribute EventHandler onscrollend;
attribute EventHandler onsecuritypolicyviolation;
attribute EventHandler onseeked;
attribute EventHandler onseeking;
attribute EventHandler onselect;
attribute EventHandler onslotchange;
attribute EventHandler onstalled;
attribute EventHandler onsubmit;
attribute EventHandler onsuspend;
attribute EventHandler ontimeupdate;
attribute EventHandler ontoggle;
attribute EventHandler onvolumechange;
attribute EventHandler onwaiting;
attribute EventHandler onwebkitanimationend;
attribute EventHandler onwebkitanimationiteration;
attribute EventHandler onwebkitanimationstart;
attribute EventHandler onwebkittransitionend;
attribute EventHandler onwheel;
};
ElementContentEditable
interface mixin ElementContentEditable {
[CEReactions] attribute DOMString contentEditable;
[CEReactions] attribute DOMString enterKeyHint;
readonly attribute boolean isContentEditable;
[CEReactions] attribute DOMString inputMode;
};
HTMLOrSVGElement
interface mixin HTMLOrSVGElement {
[SameObject] readonly attribute DOMStringMap dataset;
attribute DOMString nonce; // intentionally no [CEReactions]
[CEReactions] attribute boolean autofocus;
[CEReactions] attribute long tabIndex;
undefined focus(optional FocusOptions options = {});
undefined blur();
};
- ARIA in HTML
-
暗黙的="
No
" ARIAロール="No
" aria-* 属性="No
"暗黙的な ARIAセマンティクスがありませんが、意味はあり、この意味は、ARIA によって提供されず、ARIA のユーザーに公開されていないロール、状態、プロパティで表現される可能性があります。
作成者は、暗黙的な ARIAセマンティクス, または HTML要素のネイティブセマンティクスを上書きしてはいけません。
記述できる aria-* 属性はありません。
- レンダリング
- ボックスを生成しない
非表示の要素
script {
display: none;
}
CSS を大雑把に読むと、 要素とその子孫はボックスやテキストシーケンスを生成しません。 などになります。
- 説明
-
script
要素 は、実行できるコードを表します。script
要素 は、使用すると、作成者はドキュメントに動的スクリプトとデータブロックを含める事ができます。 この要素は、ユーザーのコンテンツを表すものではありません。<!DOCTYPE html>
<html lang=
"ja"> <head>
<meta charset=
"utf-8"> <title>script要素の例 - (Example of script element) | 離珠のHTMLでしぃ!</title>
<script src=
"./exscript01.js"></script> </head>
<body>
<h1>script要素の例</h1>
<p>この例の script要素は、./exscript01.js というスクリプトをインポートすると指定しています。</p>
</body>
</html>
このサンプルへのリンクです。
(./exscript.htm) - 参照元
-
参照元
- HTML Living Standard. 4.12.1 The script element (Last Updated 27 August 2024)
- HTML Living Standard. 15 Rendering 15.3.1 Hidden elements (Last Updated 27 August 2024)
- HTML 5.2 W3C 勧告. 4.12.1 The script element (W3C勧告、2017年12月14日)
- HTML 5.2 W3C 勧告. 10.3.1. Hidden elements (W3C勧告、2017年12月14日)
- HTML 5.1 W3C 勧告. 4.12.1 The script element (W3C勧告、2016年11月1日)
- HTML 5.1 W3C 勧告. 10.3.1. Hidden elements (W3C勧告、2016年11月1日)
- HTML 5 W3C 勧告. 4.12.1 The script element (W3C勧告、2014年10月28日)
- HTML 5 W3C 勧告. 10.3.1. Hidden elements (W3C勧告、2014年10月28日)
- HTML 4.01 Specification W3C 勧告. 18.2.1 Scripts: The SCRIPT element (W3C勧告1999年12月24日)
- HTML 3.2 Reference Specification STYLE and SCRIPT (W3C勧告1997年1月14日)
- <script>: スクリプト要素 - HTML: ハイパーテキストマークアップ言語 | MDN
- ECMAScriptR 2021 Language Specification
- JSONデータ
-
JSON形式での、データのイメージです。
{
"script": {
"check": "2024-09-01",
"group": "Metadata",
"element": "- - %Script; -- script statements. (Raw text elements.) [Metadata|Flow|Phrasing|Script] --",
"attlist": {
"%Global;": "",
"type": "%ContentType; #REQUIRED -- content type of script language --",
"src": "%URI; #IMPLIED -- URI for an external script --",
"defer": "(defer) #IMPLIED -- UA may defer execution of script --",
"nomodule": "(nomodule) #IMPLIED -- Prevents execution in user agents that support module scripts --",
"async": "(async) #IMPLIED -- Execute script when available, without blocking while fetching --",
"crossorigin": "CDATA #IMPLIED -- How the element handles crossorigin requests --",
"integrity": "CDATA #IMPLIED -- Integrity metadata used in Subresource Integrity checks [SRI] --",
"referrerpolicy": "CDATA #IMPLIED -- Referrer policy for fetches initiated by the element --",
"blocking": "%Blocking; #IMPLIED -- Whether the element is potentially render-blocking --",
"fetchpriority": "CDATA #IMPLIED -- Sets the priority for fetches initiated by the element --",
},
"comment": "スクリプト (Script statements)",
"point": "実行できるコードを表します。",
"description": "使用すると、作成者はドキュメントに動的スクリプトとデータブロックを含める事ができます。 この要素は、ユーザーのコンテンツを表すものではありません。",
"kind": "Raw",
"categories": [
"Metadata",
"Flow",
"Phrasing",
"Scriptsupporting",
],
"used": [
"parent (%Metadata)*",
"parent (%Phrasing)*",
"parent (%Scriptsupporting)*",
],
"contentmodel": "スクリプトのコンテンツ制限に一致するテキスト。",
"contentmodelsupplement": [
"src属性がない場合は、type属性の値に依存しますが、スクリプトのコンテンツ制限に一致する必要があります。",
"src属性がある場合は、その要素は空であるか、スクリプトのコンテンツ制限にも一致するスクリプトドキュメントのみを含む必要があります。",
],
"tag": [
"-",
"-",
],
"attributes": {
"src": "リソースのアドレス",
"type": "スクリプトの種類",
"nomodule": "モジュールスクリプトをサポートするユーザーエージェントでの実行を防止します",
"async": "フェッチ中にブロックせずに、可能な場合はスクリプトを実行します",
"defer": "スクリプトの実行を延期する",
"crossorigin": "要素がクロスオリジンリクエストを処理する方法",
"integrity": "サブリソース整合性チェックで使用される整合性メタデータ[SRI]",
"referrerpolicy": "要素によって開始されたフェッチのリファラーポリシー",
"blocking": "要素が潜在的にレンダリングをブロックしているかどうか",
"fetchpriority": "要素によって開始されるフェッチの優先順位を設定します。",
},
"attributessupplement": [
"charset属性がありましたが、廃止されました。作成者はscript要素にcharset属性を指定すべきではありません。属性が存在する場合、その値は 'utf-8' の大文字小文字を区別しないASCIIマッチでなければなりません。(これは、UTF-8としてエンコードされるという要件に準拠している文書では効果がありません)。属性を省略します。ドキュメントもスクリプトもUTF-8を使うことが要求されており、スクリプト要素はドキュメントを継承しているので、スクリプト要素で指定するのは冗長です。",
"language属性がありましたが、廃止されました。作成者はscript要素にlanguage属性を指定すべきではありません。属性が存在する場合、その値は文字列 'JavaScript' のASCII大文字小文字の区別のないマッチでなければならず、type属性は省略されるか、その値は文字列 'text/javascript' のASCII大文字小文字の区別のないマッチでなければなりません。この属性は、完全に省略されるか ('JavaScript' という値では、何の効果もありません)、type属性で置き換えられるべきです。JavaScriptの場合はこの属性を省略します。データ・ブロックの場合は、代わりにtype属性を使用します。",
"event属性, for属性がありましたが、廃止されました。DOM イベントメカニズムを使用してイベントリスナーを登録します。",
],
"dominterface": "HTMLScriptElement",
"rendering": "Hidden",
"reference": [
{
"title": "HTML Living Standard. 4.12.1 The script element.",
"href": "https://html.spec.whatwg.org/multipage/scripting.html#the-script-element",
"heading": "HTML Living Standard. 4.12.1 The script element",
"sub": "Last Updated 27 August 2024",
},
{
"title": "HTML Living Standard. 15 Rendering 15.3.1 Hidden elements.",
"href": "https://html.spec.whatwg.org/multipage/rendering.html#hidden-elements",
"heading": "HTML Living Standard. 15 Rendering 15.3.1 Hidden elements",
"sub": "Last Updated 27 August 2024",
},
{
"title": "HTML 5.2 W3C Recommendation. 4.12.1 The script element. W3C Recommendation, 14 December 2017.",
"href": "https://www.w3.org/TR/2017/REC-html52-20171214/semantics-scripting.html#the-script-element",
"heading": "HTML 5.2 W3C 勧告. 4.12.1 The script element",
"sub": "W3C勧告、2017年12月14日",
},
{
"title": "HTML 5.2 W3C Recommendation. 10.3.1. Hidden elements.",
"href": "https://www.w3.org/TR/2017/REC-html52-20171214/rendering.html#hidden-elements",
"heading": "HTML 5.2 W3C 勧告. 10.3.1. Hidden elements",
"sub": "W3C勧告、2017年12月14日",
},
{
"title": "HTML 5.1 W3C Recommendation. 4.12.1 The script element. W3C Recommendation, 1 November 2016.",
"href": "https://www.w3.org/TR/2016/REC-html51-20161101/semantics-scripting.html#the-script-element",
"heading": "HTML 5.1 W3C 勧告. 4.12.1 The script element",
"sub": "W3C勧告、2016年11月1日",
},
{
"title": "HTML 5.1 W3C Recommendation. 10.3.1. Hidden elements. W3C Recommendation, 1 November 2016.",
"href": "https://www.w3.org/TR/2016/REC-html51-20161101/rendering.html#hidden-elements",
"heading": "HTML 5.1 W3C 勧告. 10.3.1. Hidden elements",
"sub": "W3C勧告、2016年11月1日",
},
{
"title": "HTML 5 W3C Recommendation. 4.12.1 The script element. W3C Recommendation, 28 October 2014.",
"href": "https://www.w3.org/TR/2014/REC-html5-20141028/semantics-scripting.html#the-script-element",
"heading": "HTML 5 W3C 勧告. 4.12.1 The script element",
"sub": "W3C勧告、2014年10月28日",
},
{
"title": "HTML 5 W3C Recommendation. 10.3.1. Hidden elements. W3C Recommendation, 28 October 2014.",
"href": "https://www.w3.org/TR/2014/REC-html5-20141028/scripting-1.html#the-script-element",
"heading": "HTML 5 W3C 勧告. 10.3.1. Hidden elements",
"sub": "W3C勧告、2014年10月28日",
},
{
"title": "HTML 4.01 Specification W3C Recommendation. 18.2.1 Scripts: The SCRIPT element. W3C Recommendation 24 December 1999.",
"href": "https://www.w3.org/TR/html401/interact/scripts.html#h-18.2.1",
"heading": "HTML 4.01 Specification W3C 勧告. 18.2.1 Scripts: The SCRIPT element",
"sub": "W3C勧告1999年12月24日",
},
{
"title": "HTML 3.2 Reference Specification. W3C Recommendation 14-Jan-1997.",
"href": "https://www.w3.org/TR/2018/SPSD-html32-20180315/#style",
"heading": "HTML 3.2 Reference Specification STYLE and SCRIPT",
"sub": "W3C勧告1997年1月14日",
},
{
"title": "MDN Web Docs. HTML <script>: The Script element.",
"href": "https://developer.mozilla.org/ja/docs/Web/HTML/Element/script",
"heading": "<script>: スクリプト要素 - HTML: ハイパーテキストマークアップ言語 | MDN",
},
{
"title": "ECMAScriptR 2021 Language Specification.",
"href": "https://tc39.es/ecma262/",
"heading": "ECMAScriptR 2021 Language Specification",
},
],
"version": {
"first": "HTML3",
"latest": "HTML Living Standard",
},
}
}
注意: 簡易に描画する為に、各項目の後ろに
,
を付与しています。JSON の形式では、末尾の項目の後には,
は付けません。 - 最終確認日
-
2024-09-01
2.使用できるコンテキスト
script要素
は、
メタデータコンテンツ,
フレージングコンテンツ,
スクリプトをサポートする要素を
受け入れる要素に
配置できます。
script
メタデータ
フレージング
- h1要素
- h2要素
- h3要素
- h4要素
- h5要素
- h6要素
- p要素
- pre要素
- output要素
- progress要素
- meter要素
- label要素
- datalist要素
- body要素
- section要素
- nav要素
- aside要素
- article要素
- header要素
- footer要素
- main要素
- address要素
- li要素
- dt要素(制限あり)
- dd要素
- caption要素
- th要素(制限あり)
- td要素
- form要素
- fieldset要素
- search要素
- blockquote要素
- figure要素
- figcaption要素
- div要素
- dialog要素
- abbr要素
- b要素
- bdi要素
- bdo要素
- cite要素
- code要素
- data要素
- dfn要素
- em要素
- i要素
- kbd要素
- mark要素
- q要素
- s要素
- samp要素
- small要素
- span要素
- strong要素
- sub要素
- sup要素
- u要素
- var要素
script
script
script
script
3.コンテンツモデル
script要素
は、
動的スクリプトと
データブロックを
与えるテキストを
配置できます。
script
src
src
とりあえず
- type属性に適合したスクリプトを与えるテキスト。
- HTML文書の中としてHTMLとして扱われない文字列
- type属性の指定がスクリプト以外の時にデータブロック
スクリプトの処理内容も、規則としてはどの要素にどの要素を配置できるという制約はあるので、 どの様な記述のスクリプトをどこにでも記述できる訳ではないので、注意が必要です。 特に忘れやすいのが、そのスクリプトが実行されるタイミングでは、HTML文書がどこまで読み込まれているかという点ですのでご注意を。
4.コンテンツ属性
script要素
は、
グローバル属性と、
src, type,
nomodule,
async,
defer,
crossorigin,
integrity,
referrerpolicy,
blocking,
fetchpriority を
設定できます。
script
インポートマップスクリプト要素の場合、src
データブロックを含めるために使用する場合は、データをインラインで埋め込む必要があり、データの形式は type
charsetutf-8
の大文字小文字を区別しないASCIIマッチでなければなりません。(これは、UTF-8としてエンコードされるという要件に準拠している文書では効果がありません)。属性を省略します。ドキュメントもスクリプトもUTF-8を使うことが要求されており、スクリプト要素はドキュメントを継承しているので、スクリプト要素で指定するのは冗長です。
languageJavaScript
のASCII大文字小文字の区別のないマッチでなければならず、typetext/javascript
のASCII大文字小文字の区別のないマッチでなければなりません。この属性は、完全に省略されるか ('JavaScript' という値では、何の効果もありません)、type
event
src属性
<script src=
"./risyu.js"> </script>
src
インポートマップスクリプト要素の場合, データブロックを含めるために使用する場合は、src
この例ですが、type
type属性
<script type=
"text/javascript"> alert("ここにスクリプトが書けます。");
</script>
type
属性を省略するか、空の文字列に設定するか、JavaScript MIME タイプの本質一致に設定することは、スクリプトがクラシックスクリプトであり、JavaScriptスクリプトのトップレベルのプロダクションに従って解釈されることを意味します。 クラシックスクリプトは、src
属性を ASCII 大文字と小文字を区別しない「module
」の一致に設定すると、スクリプトが JavaScriptモジュールスクリプトであり、JavaScriptモジュールのトップレベルのプロダクションに従って解釈されることを意味します。 モジュールスクリプトは defer
この属性を「importmap
」の ASCII 大文字と小文字を区別しない一致に設定すると、スクリプトがモジュール指定子の解決の動作を制御するために使用される JSON を含むインポートマップであることを意味します。 インポートマップはインラインでのみ使用できます。つまり、src
属性を他の値に設定すると、スクリプトはデータブロックとなり、処理されません。スクリプトの属性は (タイプ自体を除く) どれもデータブロックに影響を与えません。作成者は、データブロックを示すために、JavaScript MIME タイプの本質に一致しない有効な MIME タイプ文字列を使用する必要があります。
nomodule属性
<script nomodule>
alert("ここにスクリプトが書けます。");
</script>
<script type=
"module" src= "./module.js"> </script>
nomodule
async属性
<script src=
"./risyu.js" async> </script>
async
クラシックスクリプトの場合、async
モジュールスクリプトの場合、async
この例の場合は、type
defer属性
<script src=
"./risyu.js" defer> </script>
async
クラシックスクリプトの場合、async
モジュールスクリプトの場合、async
この例の場合は、type
crossorigin属性
<script
src="https://code.jquery.com/ jquery-3.2.1.js "
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"> </script>
crossorigin
クラシックスクリプトとは異なり、モジュールスクリプトではクロスオリジンフェッチに CORSプロトコルを使用する必要があります。
この例示は、CDN(content delivery network) を使用した jQuery の読み込みの指定です。 筆者(わたし)は使っていないので実績はありませんが、この様に記述したものをコピペすると使えるそうです。 この様なものがあるという提示であり、これをそのままコピペするのはお勧めしません。また最新でもありません。
integrity属性
<script
src="https://code.jquery.com/ jquery-3.2.1.js "
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"> </script>
integrity
この例示は、CDN(content delivery network) を使用した jQuery の読み込みの指定です。 筆者(わたし)は使っていないので実績はありませんが、この様に記述したものをコピペすると使えるそうです。 この様なものがあるという提示であり、これをそのままコピペするのはお勧めしません。また最新でもありません。
referrerpolicy属性
<script
src="https://code.jquery.com/ jquery-3.2.1.js "
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous" referrerpolicy= "no-referrer-when-downgrade"> </script>
referrerpolicy
no-referrer-when-downgrade
は既定値です。この例は、とりあえず既定値だけど記述してみたというものです。
やたらなリファラー
blocking属性
<script src=
"./risyu.js" blocking= "render"> </script>
blockingrender
キーワードを指定した場合、要素は潜在的にレンダリング ブロックになります。これは個々の要素で定義されます。
デフォルトでは、要素は暗黙的にレンダリングをブロックする可能性はありません。
fetchpriority 属性
<script src=
"./main.js" fetchpriority= "high"> </script> <script src=
"./sub.js" fetchpriority= "low"> </script>
fetchpriorityhigh
, low
, auto
のキーワードがあり、それぞれ「同じ宛先を持つ他のリソースに対して優先度の高いフェッチを通知します。」「同じ宛先を持つ他のリソースに対して優先度の低いフェッチを通知します。」「同じ宛先を持つ他のリソースに対するフェッチ優先度の自動決定を通知します。」という意味です。
属性の欠損値のデフォルトと無効な値のデフォルトは両方ともauto
です。
5.DOM インターフェイス
script要素
を
直接表す
プロパティは
ありません。
scriptdocument
のgetElementById()メソッドを用いてid
<form onchange=
"document .getElementById( = document'iscJs501') .style .display .getElementById( 'iseJs501') .value;"> この下の script要素を、<select id=
"iseJs501"> <option value=
"none">表示しない</option> <option value=
"block">表示する</option> </select>。
</form>
<script id=
"iscJs501"> /* すくりぷと */
</script>
script
この例は、script
6.レンダリング
script {
display: none;
}
scriptnone
であり、この要素とその子孫はボックスやテキスト
動的スクリプトあるいはデータブロックを表すメタデータを記述する為の要素なので、ビジュアルメディアでボックスを生成する様な指定は行わないのが良いでしょう。
<script style=
"display: block;"> var vsSample = "";
/* <h1>見出し</h1> */
/*
alert("さんぷる");
*/
</script>
スクリプトの記述を見えるようにした例です。ビジュアルメディアでボックスを生成するだけで、スクリプトの機能は変わりません。スクリプトのコメントは、スクリプトとしてはコメントとして扱われています。ただ、スクリプトのコメントはHTMLでは関係ないので、そのまま見えます。また、これはテキストとして扱われているので、そこに書かれているHTMLタグはテキスト文字列として扱われ、タグとは扱われません。
とはいえ、script-->
があった場合も同じです。
7.気を付ける点
記述順
script
たとえば、とあるグローバル変数の更新を扱う場合のタイミングの問題とか。
記述によって、いつ処理するか, 何が対象か, どこまで影響するか などが異なる為、内容を意識しないと配置できません。
出来れば、スクリプト自体をscript
簡単な転用
script
文字結合
script-->
を扱いたい場合は、--
と >
とを別々の文字列として記述して、結合して扱うべきです。通常は文字列として扱われていても <!--
からコメントの終わりを探されている途中に文字列の -->
があると文字列として扱われずにコメントの終わりの記述として扱われてしまいます。
あくまで、HTML上で <!--
でコメントが始まり、script
文字列として </script>
を扱いたい場合は、<
と /script>
とを別々の文字列として記述して、結合して扱うべきです。script
<script>
var lfbEvSample701 = function(){
alert("--" + ">" + " " + "<" + "/script>");
}
</script>
<div onclick=
"lfbEvSample701();">click</div>
HTML文書でのコメントの終わりを示す -->
と、script</script>
の文字列をスクリプト内で用いる例です。