Skip to main content

HTML DOM scripts Collection in JAVA SCRIPT

Definition and Usage..
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.
Syntax: document.scripts
CODING:
<!DOCTYPE html>
<html>
<body>
<script>
document.write("I am Script");
</script>
<p>Click the button to get the contents of the first script element in the document.</p>
<p id="demo"></p>
<button onclick="count()">Total Script</button>
<button onclick="view1()">View 1st Script</button>
<button onclick="view2()">View 2nd Script</button><br>
<script id="xyz">
    document.write("I am Script");
function count() {
    document.getElementById("demo").innerHTML =document.scripts.length}
function view1() {
    var x = document.scripts.item(0).text;
    document.getElementById("demo").innerHTML = x;
     }
    function view2() {
    var y = document.scripts.namedItem("xyz").text;
    document.getElementById("demo").innerHTML = y;
     }
</script>

</body>
</html>

Comments