document.scripts returns a live HTMLCollection of every <script> element in the document (MDN). You can use it like an array — length, indexes, and loops.
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax
Use document.scripts. Start with this line.
JavaScript
document.scripts
Call
What it does
document.scripts
Primary signature.
Built into the browser. Read or assign document.scripts — no import required.
Library declarations
JavaScript
document.scripts
Parameters
Parameter
Description
HTMLCollection
Required. Live list of <script>
Writable?
Optional. Read-only property — mutate the DOM instead
Return Value
Return Value
Description
*
Live HTMLCollection of HTMLScriptElement
JavaScript Examples
Example 1 — Does the Page Have Scripts? (MDN)
MDN pattern: check document.scripts.length.
JavaScript
const scripts = document.scripts;
if (scripts.length) {
console.log("This page has scripts!");
console.log("count:", scripts.length);
}
Iterate with for…of — each value is an HTMLScriptElement.
Prefer getAttribute("src") for the authored path; empty means inline.
Read type — default classic scripts often omit it.
Tip:s.src alone is usually an absolute URL for external scripts.
Example 3 — The Collection Is Live
Append a <script> and watch document.scripts.length update.
JavaScript
const list = document.scripts;
console.log(list.length); // before
const s = document.createElement("script");
s.textContent = "// demo";
document.body.appendChild(s);
console.log(list.length); // after — increased by 1