JavaScript Document defaultView Property

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

What You’ll Learn

Document.defaultView is a read-only instance property that returns the Window associated with a document, or null if none is available. Learn how it relates to the global window, when it is null, how iframes fit in, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

Window | null

03

Alias

Often window

04

Use when

Have a Document

05

Iframes

contentWindow

06

Status

Baseline widely

Introduction

Every displayed HTML page has a Document (the DOM tree) and a Window (the browsing context: viewport size, location, alert, timers, and so on).

MDN: in browsers, document.defaultView returns the window object associated with that document, or null if none is available. On a normal page, document.defaultView === window is true.

💡
When to prefer defaultView

If your function receives a Document (from an iframe, a shadow host’s owner document, or a library), use doc.defaultView to reach that document’s Window—do not assume the global window is the right one.

Related Document tutorials: customElementRegistry, currentScript, Document constructor.

Understanding Document.defaultView

A read-only instance property on Document. Its value is a Window (browsing context) or null.

  • Main page — usually the same object as global window.
  • Read-only — you cannot assign a new view (MDN).
  • May be null — document not tied to a browsing context.
  • Iframes — nested document’s defaultView matches that frame’s window when accessible.
  • Useful APIsinnerWidth, getComputedStyle, matchMedia, scrollTo via the Window.

📝 Syntax

JavaScript
document.defaultView

Value

The associated Window object, or null if none is available (MDN).

Typical check

JavaScript
const win = document.defaultView;
if (win) {
  console.log(win.innerWidth);
} else {
  console.log("No browsing context for this document");
}

⚡ Quick Reference

GoalCode / note
Get Window from Documentdocument.defaultView
Same as global?document.defaultView === window
Safe widthdocument.defaultView?.innerWidth
Computed styledoc.defaultView.getComputedStyle(el)
No windowValue may be null
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.defaultView.

Type
Window | null

Or null

Access
read-only

No setter

Main page
=== window

Usually

Status
baseline

Standard API

📋 Document ↔ Window links

DirectionPropertyMeaning
Document → Windowdocument.defaultViewBrowsing context for this document
Window → Documentwindow.documentDOM tree displayed in this window
Node → Documentnode.ownerDocumentWhich document owns the node

Examples Gallery

Examples follow MDN Document: defaultView. Use View Output or Try It Yourself for each case.

📚 Getting Started

Confirm the link between Document and Window.

Example 1 — Same as Global window

On a normal page, defaultView is the global window object.

JavaScript
console.log(document.defaultView === window); // true
console.log(document.defaultView.document === document); // true
Try It Yourself

How It Works

Document and Window point at each other: defaultView and window.document.

Example 2 — Read Viewport Size via defaultView

Use Window APIs without hard-coding the global window name.

JavaScript
const win = document.defaultView;
console.log("innerWidth:", win.innerWidth);
console.log("innerHeight:", win.innerHeight);
Try It Yourself

How It Works

Any Window property (scrollY, devicePixelRatio, …) is available through defaultView.

📈 Styles, Detached Docs & Helpers

Computed styles, null checks, and a reusable helper.

Example 3 — getComputedStyle via defaultView

Libraries often take a document and need its window for styles.

JavaScript
const el = document.body;
const win = document.defaultView;
const styles = win.getComputedStyle(el);
console.log("body display:", styles.display);
Try It Yourself

How It Works

getComputedStyle lives on Window, not on Document—so you go through defaultView.

Example 4 — Created Documents May Differ

A document from createHTMLDocument still often has a defaultView in browsers, but always guard for null.

JavaScript
const fresh = document.implementation.createHTMLDocument("Scratch");
console.log("Main defaultView null?", document.defaultView === null);
console.log("Fresh defaultView:", fresh.defaultView);
// May be a Window, or null depending on environment — always check
Try It Yourself

How It Works

MDN allows null when no window is available. Production code should use optional chaining or an explicit null check.

Example 5 — Helper That Accepts Any Document

Reusable pattern for code that must not assume the global window.

JavaScript
function getViewportWidth(doc = document) {
  const win = doc.defaultView;
  if (!win) return null;
  return win.innerWidth;
}

console.log("Width:", getViewportWidth());
console.log("Width (explicit):", getViewportWidth(document));
Try It Yourself

How It Works

Pass iframe.contentDocument into the same helper to measure that frame’s viewport when cross-origin rules allow.

🚀 Common Use Cases

  • Multi-document libraries — resolve Window from a given Document.
  • iframe tooling — work with contentDocument.defaultView when accessible.
  • Computed styles — call getComputedStyle on the correct Window.
  • Responsive helpers — read innerWidth / matchMedia for that document’s view.
  • Testing / SSR awareness — handle null when no browsing context exists.
  • Teaching Document ↔ Window — show the bidirectional link with window.document.

🧠 How Document Links to Window

1

Browsing context opens

Browser creates a Window for the tab or frame.

Window
2

Document is associated

The loaded page becomes window.document.

Document
3

defaultView points back

From any Document reference you can recover its Window.

Link
4

Or null if detached

No browsing context → MDN allows null; check before use.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Read-only; returns Window or null.
  • On normal pages, equals the global window.
  • Cross-origin iframes may block access to contentDocument / nested defaultView.
  • Related: customElementRegistry, currentScript, ownerDocument, Document constructor.

Universal Browser Support

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

Baseline · Widely available

Document.defaultView

Read-only Window associated with the document — or null if none is available.

Universal 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 (legacy)
Full support
Document.defaultView Excellent

Bottom line: Use document.defaultView to get the Window for a Document reference. On the main page it matches window; always allow for null.

Conclusion

Document.defaultView is the standard bridge from a Document back to its Window. Use it whenever you hold a document reference and need viewport, style, or other Window APIs—and remember it can be null.

Continue with designMode, customElementRegistry, currentScript, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use doc.defaultView when your API receives a Document
  • Null-check or use optional chaining (?.)
  • Prefer it over assuming global window in shared helpers
  • Pair with window.document to explain the bidirectional link
  • Respect cross-origin limits on iframe documents

❌ Don’t

  • Assign to document.defaultView
  • Call Window methods without checking for null
  • Confuse defaultView with document.body or viewport meta
  • Assume every Document is the main page document
  • Ignore cross-origin iframe access errors

Key Takeaways

Knowledge Unlocked

Five things to remember about document.defaultView

Document → Window bridge — often the same as window.

5
Core concepts
02

Status

baseline

Standard
🔒03

Access

read-only

DOM
🔀04

Main page

=== window

Common
🔗05

Inverse

window.document

Link

❓ Frequently Asked Questions

In browsers, the Window object associated with the document, or null if none is available. On a normal web page it is the same object as the global window.
No. MDN marks Document.defaultView as Baseline Widely available (since July 2015). It is a standard read-only Document instance property.
On the main page document, yes — document.defaultView === window is typically true. Prefer defaultView when you only have a Document reference (for example from another frame or a library API).
When the document is not associated with a browsing context / window — for example some programmatically created documents that were never displayed.
An iframe has contentDocument (the nested Document) and contentWindow (its Window). That nested document's defaultView is the same as the iframe's contentWindow when accessible.
No. The property is read-only (MDN).
Did you know?

The name defaultView comes from older DOM / CSSOM ideas of a “view” of a document. In everyday browser code it simply means “the Window for this Document.”

Next: designMode

Learn how to make an entire document editable with on/off.

designMode →

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