HTML | DOM scripts Collection

The DOM scripts Collection in HTML is used to return the collection of all <script> element in HTML document. The script elements are sorted as appear in the sourcecode.

Syntax:

document.scripts

Property Value: It returns the single value length which is the collection of all script element.

Method:

  • [index]: It is used to return the <script> element of selected index. The index value starts with 0. It returns NULL if the index value is out of range.
  • item(index): It is used to return the <script> element of selected index. The index value starts with 0. It returns NULL if the index value is out of range.
  • namedItem(id): It is used to return the <script> element from the collection with given id attribute. It returns NULL if the id is not valid.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <title>DOM scripts collection</title
          
        <style
            .gfg { 
                font-size:40px; 
                font-weight:bold; 
                color:green; 
            
            body { 
                text-align:center; 
                font-family:Times; 
            
        </style
    </head
      
    <body
        <a name="" class="gfg">GeeksforGeeks</a
          
        <h2>DOM scripts Collection</h2>
          
        <!-- script content -->
        <script>
            document.write("GeeksForGeeks");
        </script><br><br>
          
        <button onclick="Geeks()">Submit</button>
          
        <p id="sudo"></p
          
        <!-- script content -->
        <script
            function Geeks() { 
                var x = document.scripts.length; 
                document.getElementById("sudo").innerHTML = 
                "Number of scripts elements: " + x; 
            
        </script
          
    </body
</html>                                 

chevron_right


Output:
Before Click on the button:

After Click on the button:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <title>DOM scripts collection</title
          
        <style
            .gfg { 
                font-size:40px; 
                font-weight:bold; 
                color:green; 
            
            body { 
                text-align:center; 
                font-family:Times; 
            
        </style
    </head
      
    <body
        <a name="" class="gfg">GeeksforGeeks</a
          
        <h2>DOM scripts Collection</h2>
          
        <!-- script content -->
        <script>
            document.write("GeeksForGeeks");
        </script><br><br>
          
        <button onclick = "Geeks()">Submit</button
          
        <p id="sudo"></p
          
        <!-- script content -->
        <script
            function Geeks() { 
                var x = document.scripts[0].text;; 
                document.getElementById("sudo").innerHTML = 
                "First script Element: " + x;
            
        </script
          
    </body
</html>                                         

chevron_right


Output:
Before Click on the button:

After Click on the button:

Supported Browsers: The browser supported by DOM scripts collection are listed below:

  • Apple Safari
  • Google Chrome
  • Firefox 9.0
  • Opera
  • Internet Explorer


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.