English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM scripts attribute

HTML DOM Document Object

scriptsThe attribute returns<script>List of elements.

The elements in the list are sorted in the order they appear in the source code.

Syntax:

document.scripts
var x = document.scripts.length;
Test and see‹/›

Browser compatibility

All browsers fully support the scripts attribute:

Attribute
scriptsIsIsIsIsIs

Property

AttributeDescription
lengthReturn the list in<script>Number of elements

Method

MethodDescription
[index]Return the specific node at the given zero-based index in the list. If the index number is out of range, return null.
item(index)Return the specific node at the given zero-based index in the list. If the index number is out of range, return null.
namedItem(id)Returns the specific node whose ID name matches the string specified by name. If the id does not exist, it returns null.

Technical Details

Return Value:oneHTMLCollection. You can use it like an array to get all elements in the list
DOM Version:DOM Level3

More Examples

Display the content of the first script element in the document (index 0):

var x = document.scripts[0].text;
Test and see‹/›

Traverse all script elements and display the text content of each script:

var my_list = document.scripts;
for (let i = 0; i < my_list.length;++) {
   document.getElementById("x").innerHTML += my_list[i].text + "<br>";
}
Test and see‹/›

HTML DOM Document Object