HTMLDOMドキュメントスクリプト
例
ドキュメント内の<script>要素の数:
document.scripts.length;
最初の<script>要素の内容を返します。
document.scripts[0].text;
最初の<script>要素の内容を返します。
document.scripts.item(0).text;
以下のその他の例。
定義と使用法
このscripts
プロパティは、ドキュメント内のすべての<script>要素のコレクションを返します。
scripts
プロパティはHTMLCollectionを返します。
scripts
プロパティは読み取り専用です。
関連項目:
HTMLCollection
HTMLCollectionは、HTMLノードのコレクションです。
コレクション内のノードには、インデックス番号でアクセスできます。インデックスは0から始まります。
lengthプロパティは、コレクション内の要素の数を返します。
構文
document.scripts
プロパティ
財産 | 説明 |
長さ | コレクション内の<script>要素の数。 |
メソッド
Method | Description |
[index] | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
item(index) | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
namedItem(id) | Returns the element with the specified id. Returns null if the id does not exist. |
戻り値
タイプ | 説明 |
物体 | HTMLCollectionオブジェクト。 ドキュメント内のすべての<script>要素。 要素は、ドキュメントに表示されるとおりに並べ替えられます。 |
その他の例
<script>要素のコンテンツをid = "myScript"で返します。
document.scripts.namedItem("myScript").text;
すべての<script>要素をループし、各IDを出力します。
const collection = document.scripts;
let text = "";
for (let i = 0; i < collection.length; i++) {
text += collection[i].id + "<br>";
}
ブラウザのサポート
document.scripts
DOMレベル2(2001)の機能です。
すべてのブラウザで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |