HTML DOM scriptsCollection

Document 对象参考手册Document Object

Example

See how many <script> elements are in the document:

var x = document.scripts.length;

Try it Yourself »

Definition and Usage

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

Note:The elements in the collection are ordered as they appear in the source code.

Tip:Related ContentScript Object。


Browser Support

The numbers in the table specify the first browser version that fully supports the collection.

Collection
scripts Yes Yes 9.0  Yes Yes

Syntax

document.scripts

Properties

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

Tip:This is a read-only property.

Methods

Method Description
[index] Returns the <script> element in the collection with the specified index (starting at 0).

Note:Returns null if the index is out of range.
item(index) Returns the <script> element in the collection with the specified index (starting at 0).

Note:Returns null if the index is out of range.
namedItem(id) Returns the <script> element in the collection with the specified id.

Note:Returns null if the id does not exist.

Technical Details

DOM Version: Core Level 3 Document Object
Return Value: An HTMLCollection object, representing all <script> elements in the document. The elements in the collection are sorted according to their order in the source code.

More Examples

Example

[index]

Get the content of the first <script> element (index 0) in the document:

var x = document.scripts[0].text;

Try it Yourself »

Example

item(index)

Get the content of the first <script> element (index 0) in the document:

var x = document.scripts.item(0).text;

Try it Yourself »

Example

namedItem(id)

Get the content of the <script> element with id="example":

var x = document.scripts.namedItem("example").text;

Try it Yourself »

Example

Loop through all <script> elements in the document and output the id of each <script> element:

var x = document.scripts; var txt = ""; var i; for (i = 0; i < x.length; i++) { txt = txt + x[i].id + "<br>"; }

Try it Yourself »

Related Articles

JavaScript Reference:HTML DOM Script Object

HTML Tutorial:HTML Scripts

HTML Reference:HTML <script> Tag


Document 对象参考手册Document Object

Other Extensions