Document.scripts - script要素
scriptsは、Documentのプロパティです。ドキュメントの全てのscript要素を返します。
概要
- 名前
- scripts
- 所属
- Document
- IDL
[SameObject] readonly attribute HTMLCollection scripts;
- 仕様書
- https://html.spec.whatwg.org/multipage/dom.html#dom-document-scripts
説明
全てのscript要素をHTMLCollectionで返す。読み取り専用なので、値を代入して変更することはできない。
チュートリアル
Document.scriptsを利用して、script要素を取得する例です。
<script>console.log("hoge")</script>
<script>console.log("fuga")</script>
var scripts = document.scripts ;
scripts[0] ; // <script>console.log("hoge")</script>
scripts[1] ; // <script>console.log("fuga")</script>
Document.getElementsByTagName()と同じです。
var scripts = document.scripts ;
var scripts = document.getElementsByTagName( "script" ) ;
デモ
Document.scriptsのデモです。
<!-- このコードは編集できます。 -->
<!DOCTYPE html>
<html>
<body>
<script>console.log("hoge")</script>
<script>console.log("fuga")</script>
<hr>
<script>
var value = document.scripts ;
console.log( value ) ;
document.body.appendChild( new Text( value ) ) ;
</script>
</body>
</html>
サポート状況
Chrome | Firefox | Safari | Edge | IE | Opera | iOS Safari | Android |
---|---|---|---|---|---|---|---|
● | ● 9+ | ● | ● | ● | ● | ● | ● |
関連記事
- Document.currentScript
- currentScriptは、Documentのプロパティです。現在、処理を実行しているscript要素を返します。存在しない場合、値はnullになります。
- Document.head
- headは、Documentのプロパティです。ドキュメントのhead要素を返します。
- Document.createElement()
- createElement()は、Documentのメソッドです。タグ名を指定して、要素を新しく作成します。
- Document.getElementsByTagNameNS()
- getElementsByTagNameNS()は、Documentのメソッドです。タグ名と名前空間を指定して要素を取得します。