HTML DOM scripts Collection

HTMLWeb DevelopmentFront End Technology

The HTML DOM Script collection returns the collection of all <script> elements of an HTML document.

Syntax

Following is the syntax −

document.scripts

Property of script object

PropertyExplanation
lengthIt returns the number of <script> element in the collection in a HTML document.

Methods of script object

MethodExplanation
[index]It returns the specified index <script> element from the collection.
item(index)It returns the specified index <script> element from the collection.
namedItem(id)It returns the specified id <script> element from the collection.

Example

Let us see an example of scripts collection −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   html{
      height:100%;
   }
   body{
      text-align:center;
      color:#fff;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
      height:100%;
   }
   p{
      font-weight:700;
      font-size:1.2rem;
   }
   ul{
      list-style-type: none;
      padding:0;
   }
   .btn{
      background:#0197F6;
      border:none;
      height:2rem;
      border-radius:2px;
      width:50%;
      margin:2rem auto;
      display:block;
      color:#fff;
      outline:none;
      cursor:pointer;
   }
   .show{
      font-size:1.5rem;
      font-weight:bold;
   }
</style>
</head>
<body>
<script>
console.log("script one");
</script>
<h1>DOM scripts Collection Demo</h1>
<p>Hi, There are two script elements on this page:</p>
<script>
console.log("script two");
</script>
<button onclick="show()" class="btn">Show No. Of Script</button>
<div class='show'></div>
<script>
   function show() {
      var scriptList = document.scripts;
      document.querySelector(".show").innerHTML=scriptList.length;
   }
</script>
</body>
</html>

Output

This will produce the following output −

Click on “Show No. of Script” button to display the number of scripts elements.

raja
Published on 19-Jul-2019 14:30:23
Advertisements