Javascript examples for DOM:Document scripts
The scripts collection returns all <script> elements in the document sorted as they appear in the source code.
Property | Description |
---|---|
length | Returns the number of <script> elements in the collection. |
Method | Description |
---|---|
[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. |
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:
<!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>