Easy Tutorial
❮ Event Onclick Prop Win Defaultstatus ❯

HTML DOM scripts Collection

Document Object

Example

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

var x = document.scripts.length;

Definition and Usage

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

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

Tip: Related content Script Object.


Browser Support

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

Collection Chrome Edge Firefox Safari Opera
scripts Yes Yes 9.0 Yes Yes

Syntax

document.scripts

Properties

Property Description
length Returns the number of <script> elements in the collection. <br> <br> Tip: This is a read-only property.

Methods

Method Description
[index] Returns the <script> element at the specified index (starting from 0). <br> <br> Note: Returns null if the index is out of range.
item(index) Returns the <script> element at the specified index (starting from 0). <br> <br> Note: Returns null if the index is out of range.
namedItem(id) Returns the <script> element with the specified id. <br> <br> 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 are ordered as they appear in the source code.

More Examples

Example

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

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

Example

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

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

Example

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

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

Example

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

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

Related Articles

JavaScript Reference: HTML DOM Script Object

HTML Tutorial: HTML Scripts

HTML Reference: HTML <script> Tag


Document Object

❮ Event Onclick Prop Win Defaultstatus ❯