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
Fundamentals
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.
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported (legacy)
Full support
Document.defaultViewExcellent
Bottom line: Use document.defaultView to get the Window for a Document reference. On the main page it matches window; always allow for null.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.defaultView
Document → Window bridge — often the same as window.
5
Core concepts
🖥️01
Returns
Window | null
API
✓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.”