HTML DOM Document scripts Property

Definition and Usage

scripts The property returns the collection of all <script> elements in the document.

scripts The property returns HTMLCollection.

scripts The property is read-only.

See also:

Script Object

HTMLCollection

HTMLCollection It is a similar array collection (list) of HTML elements.

Elements in the collection can be accessed by index (starting from 0).

length The property returns the number of elements in the collection.

Instance

Example 1

The number of <script> elements in the document:

document.scripts.length;

Try it yourself

Example 2

Returns the content of the first <script> element:

document.scripts[0].text;

Try it yourself

Example 3

Returns the content of the first <script> element:

document.scripts.item(0).text;

Try it yourself

Example 4

Returns the content of the <script> element with id="myScript":

document.scripts.namedItem("myScript").text;

Try it yourself

Example 5

Traverse all <script> elements and output each id:

const collection = document.scripts;
let text = "";
for (let i = 0; i < collection.length; i++) {
  text += collection[i].id + "<br>";
}

Try it yourself

Syntax

document.scripts

Property

Property Description
length The number of <script> elements in the collection.

Method

Method Description
[index]

Returns the element with the specified index (starting from 0).

Returns null if the index is out of range.

item(index)

Returns the element with the specified index (starting from 0).

Returns null if the index is out of range.

namedItem(id)

Returns an element with the specified id element.

If id Returns null if it does not exist.

Returns

Type Description
Object

HTMLCollection object.

All <form> elements in the document.

Sorted in the order they appear in the source code.

Browser support

document.scripts It is a DOM Level 2 (2001) feature.

All browsers support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support

Related pages

HTML DOM Script Object

HTML Script Tutorial

HTML <script> Tag