Javascript DOM HTML document scripts Collection

Introduction

Find out how many <script> elements there are in the document:

var x = document.scripts.length;

Click the button to display the number of script elements in the document.

View in separate window

<!DOCTYPE html>
<html>
<body>
<script>
document.write("Hello World!");
</script>/* w  w w  .  j  a  v a 2  s.co  m*/
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {
  var x = document.scripts.length;
  document.getElementById("demo").innerHTML = "Found " + x + " script elements.";
}
</script>

</body>
</html>

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

The elements in the collection are sorted as they appear in the source code.

Properties

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

This property is read-only

Methods

Method
Description
[index]

Returns the <script> element from the collection with the index starting at 0.
Returns null if the index number is out of range
item(index)

Returns the <script> element from the collection with the index starting at 0.
Returns null if the index number is out of range
namedItem(id)

Returns the <script> element from the collection with the specified id.
Returns null if the id does not exist



PreviousNext

Related