Find out how many <script> elements there are in the document:
The result of x will be:
More "Try it Yourself" examples below.
The scripts collection returns a collection of all <script> elements in the document.
Note: The elements in the collection are sorted as they appear in the source code.
Tip: Also look at the Script Object.
The numbers in the table specify the first browser version that fully supports the collection.
Collection | |||||
---|---|---|---|---|---|
scripts | Yes | Yes | 9.0 | Yes | Yes |
Property | Description |
---|---|
length | Returns the number of <script> elements in the collection. Note: This property is read-only |
Method | Description |
---|---|
[index] | Returns the <script> element from the collection with the specified index
(starts at 0). Note: Returns null if the index number is out of range |
item(index) | Returns the <script> element from the collection with the specified index
(starts at 0). Note: Returns null if the index number is out of range |
namedItem(id) | Returns the <script> element from the collection with the specified id. Note: Returns null if the id does not exist |
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 as they appear in the source code |
[index]
Get the contents of the first <script> element (index 0) in the document:
The result of x will be:
item(index)
Get the contents of the first <script> element (index 0) in the document:
The result of x will be:
namedItem(id)
Get the contents of the <script> element with id="myScript" in the document:
The result of x will be:
Loop through all <script> elements in the document, and output the id of each script:
The result of txt will be:
JavaScript reference: HTML DOM Script Object
HTML tutorial: HTML Scripts
HTML reference: HTML <script> tag