Document scripts Collection - Javascript DOM

Javascript examples for DOM:Document scripts

Description

The scripts collection returns all <script> elements in the document sorted as they appear in the source code.

Syntax

Properties

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

Methods

MethodDescription
[index] <script> element with the specified index (starts at 0).
item(index) <script> element with the specified index (starts at 0).
namedItem(id) <script> element with the specified id.

Technical Details

Return Value:

An HTMLCollection Object, representing all <script> elements in the document.

The following code shows how to Find out how many <script> elements there are in the document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<script id="myFirstScript">
document.write("Hello World!");
</script>/*from   w w w. j  a va 2s . c om*/

<button type="button" onclick="myFunction()">Test</button>

<p id="demo"></p>

<script id="mySecondScript">
function myFunction() {
    var x = document.scripts;
    var txt = "";
    var i;
    for (i = 0; i < x.length; i++) {
        txt = txt +  x[i].id + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Related Tutorials