JavaScript Document scripts Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.scripts is a read-only instance property that returns a live HTMLCollection of every <script> in the document. Learn how to count scripts, read src and type, compare with querySelectorAll, and five examples with try-it labs.

01

Kind

Read-only property

02

Returns

HTMLCollection

03

Items

HTMLScriptElement

04

Access

Index like array

05

Live

Updates with DOM

06

Status

Baseline widely

Introduction

Modern pages load JavaScript from many places—inline blocks, bundled files, analytics snippets, and module scripts. When your code needs to inspect every <script> tag on the page, document.scripts is the built-in collection to use.

MDN: the scripts property returns a list of the <script> elements in the document. The returned object is an HTMLCollection. You can use it just like an array to get all the elements in the list.

💡
Not the same as currentScript

document.scripts lists every script tag. document.currentScript returns only the one classic script currently being processed (or null in callbacks).

Related Document tutorials: currentScript, forms, images, Document constructor.

Understanding Document.scripts

A read-only instance property on every Document object.

  • Value — live HTMLCollection of HTMLScriptElement nodes (MDN).
  • Includes — inline scripts and external scripts with a src attribute.
  • Accessdocument.scripts.length, document.scripts[0], or loop with for...of.
  • Live — adding or removing <script> tags updates the same collection.
  • Status — Baseline Widely available (since June 2018, MDN).

📝 Syntax

JavaScript
document.scripts

Value

An HTMLCollection of HTMLScriptElement items (MDN).

MDN example pattern

JavaScript
let scripts = document.scripts;

if (scripts.length) {
  console.log("This page has scripts!");
}

⚡ Quick Reference

GoalCode / note
Get collectiondocument.scripts
Count scriptsdocument.scripts.length
First scriptdocument.scripts[0]
Check if any existdocument.scripts.length > 0
External URLscript.src on each item
Module script?script.type === "module"

🔍 At a Glance

Four facts about document.scripts.

Type
HTMLCollection

Read-only

Items
HTMLScriptElement

Each tag

Live
true

DOM sync

Status
Baseline

Since 2018

📋 scripts vs querySelectorAll("script")

document.scriptsquerySelectorAll("script")
Return typeHTMLCollectionNodeList
Live updatesYesNo (static snapshot)
Document-specific APIYesGeneric selector API
MDN statusBaseline Widely availableBaseline Widely available

Examples Gallery

Examples follow MDN Document: scripts. Each includes a try-it lab you can run in the browser.

📚 Getting Started

Read and inspect the script collection.

Example 1 — Check If the Page Has Scripts (MDN)

MDN’s classic pattern: test scripts.length before doing work.

JavaScript
let scripts = document.scripts;

if (scripts.length) {
  console.log("This page has scripts!");
}
Try It Yourself

How It Works

Any page with at least one <script> tag (including the try-it lab itself) passes this check.

Example 2 — Count Script Tags

Use length to see how many scripts are on the page.

JavaScript
const count = document.scripts.length;
console.log("Script count:", count);
Try It Yourself

How It Works

The number includes inline scripts and external scripts loaded via src.

📈 Practical Patterns

Loop, filter, and inspect script elements.

Example 3 — List External Script URLs

Loop the collection and collect src values.

JavaScript
const urls = [];

for (const script of document.scripts) {
  if (script.src) {
    urls.push(script.src);
  }
}

console.log(urls);
Try It Yourself

How It Works

Inline scripts have an empty src; external files return the full URL string.

Example 4 — Separate Module and Classic Scripts

Check the type attribute on each script element.

JavaScript
let modules = 0;
let classic = 0;

for (const script of document.scripts) {
  if (script.type === "module") {
    modules++;
  } else {
    classic++;
  }
}

console.log({ modules, classic });
Try It Yourself

How It Works

Scripts without type="module" are treated as classic JavaScript (default or empty type).

Example 5 — Live Collection After DOM Change

Add a script tag and watch document.scripts.length grow.

JavaScript
const before = document.scripts.length;

const extra = document.createElement("script");
extra.textContent = "console.log('added');";
document.body.appendChild(extra);

const after = document.scripts.length;
console.log("before:", before, "after:", after);
Try It Yourself

How It Works

A static querySelectorAll("script") snapshot would not grow unless you call it again.

🚀 Common Use Cases

  • Debug inventory — count how many scripts loaded on a page.
  • Audit third-party tags — list external src URLs.
  • Performance reviews — find blocking scripts without async or defer.
  • Module migration — count classic vs type="module" scripts.
  • Feature detection — confirm scripts exist before running loader logic.
  • Teaching DOM collections — compare live HTMLCollection with static NodeList.

🧠 How document.scripts Works

1

HTML contains <script> tags

Authors place inline or external scripts anywhere in the document tree.

Markup
2

Browser builds a filtered collection

HTML: an HTMLCollection of script elements rooted at the Document (MDN).

Filter
3

You read by index or loop

scripts.length, scripts[0], or for...of.

Access
4

DOM changes stay in sync

Add or remove script tags and the same collection reflects the new set.

📝 Notes

  • MDN: Baseline Widely available (since June 2018) — no Deprecated / Experimental / Non-standard banner.
  • Value is an HTMLCollection of HTMLScriptElement items.
  • Not the same as document.currentScript (single running script only).
  • Includes dynamically inserted scripts once they are in the document tree.
  • Related: currentScript, forms, Document constructor.

Browser Support

Document.scripts is marked Baseline Widely available on MDN (since June 2018). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.scripts

Read-only live HTMLCollection of every <script> — count, index, and inspect src/type.

Widely Available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE
Full support
Document.scripts Baseline support

Bottom line: Use document.scripts to list every script tag on a page. Pair with currentScript when you need the one script currently executing.

Conclusion

Document.scripts is the standard, live list of every <script> in a document. Use length to count tags, loop to inspect src and type, and remember it updates when the DOM changes.

Continue with scrollingElement, currentScript, forms, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use document.scripts for a live list of all scripts
  • Check length before assuming scripts exist
  • Read src, async, defer, and type per item
  • Pair with currentScript when debugging execution order
  • Prefer for...of to loop the collection clearly

❌ Don’t

  • Confuse scripts with currentScript
  • Assume a static snapshot if you mutate the DOM later
  • Expect scripts to include scripts not yet in the tree
  • Rely on script count alone for security audits
  • Modify the collection directly (it is read-only)

Key Takeaways

Knowledge Unlocked

Five things to remember about document.scripts

Live HTMLCollection of every script tag on the page.

5
Core concepts
📝02

Items

HTMLScriptElement

Type
🔄03

Live

DOM sync

Collection
📊04

Count

.length

MDN
05

Status

Baseline

2018+

❓ Frequently Asked Questions

A read-only HTMLCollection of every <script> element in the document. Each item is an HTMLScriptElement. MDN: you can use it like an array to get all elements in the list.
No. MDN marks Document.scripts as Baseline Widely available (since June 2018). It is a standard Document collection property.
document.scripts lists every <script> tag in the document. document.currentScript returns only the one classic script currently being processed (or null).
Yes. HTMLCollection updates when script elements are added or removed from the document.
Both work. document.scripts is the dedicated Document API and returns a live HTMLCollection. querySelectorAll returns a static NodeList snapshot.
Yes. Any <script> element in the document tree is included, whether it has a src attribute or inline code.
Did you know?

MDN says you can use document.scripts just like an array. That means familiar patterns—length, bracket indexing, and for...of loops—work without converting to a real array first.

Next: scrollingElement

Learn which Element scrolls the document and how to reset scroll position.

scrollingElement →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful