YouTip LogoYouTip

Coll Doc Scripts

## HTML DOM Document.scripts Collection The `document.scripts` collection returns an ordered list of all ` ``` ```javascript // Retrieve the script element with id="app-config" var configScript = document.scripts.namedItem("app-config").text; console.log(configScript); ``` ### Example 4: Iterating Through All Script Elements You can loop through the collection to inspect attributes (such as `id` or `src`) of every script on the page: ```javascript // Get all script elements var scripts = document.scripts; var output = ""; // Loop through the collection and collect their IDs for (var i = 0; i < scripts.length; i++) { var scriptId = scripts.id ? scripts.id : "No ID"; output += "Script #" + i + " ID: " + scriptId + "\n"; } console.log(output); ``` --- ## Technical Details * **DOM Specification:** DOM Level 3 Core Document Object. * **Live Collection:** Because `HTMLCollection` is live, any script elements appended via `document.createElement('script')` and `document.body.appendChild()` will immediately reflect in `document.scripts`. --- ## Related Documentation * (./dom-obj-script.html) * [HTML