Document.domain is a deprecated instance property that gets or sets the domain portion of the origin for same-origin checks. Learn what the getter returns, why the setter is unsafe, safer alternatives, and five examples with try-it labs.
01
Kind
Get / set
02
Type
string
03
Status
Deprecated
04
Read via
location.hostname
05
Talk via
postMessage
06
Risk
Setter unsafe
Fundamentals
Introduction
Browsers isolate pages with the same-origin policy. An origin is roughly scheme + host + port (for example https://a.example.com:443). Two pages can normally read each other’s DOM only if they share that origin.
Historically, sites on a.example.com and b.example.com sometimes set document.domain = "example.com" so both pages shared a relaxed domain and could access each other’s DOM. MDN now deprecates that pattern because it is hard to reason about and easy to get dangerously wrong.
💡
Modern replacements
Read the host with location.hostname. For cross-window talk, use postMessage. Do not open your whole DOM to every sibling subdomain.
Document.domain is deprecated on MDN. It may still exist for compatibility, but the setter is unsafe and should not be used in new code. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Deprecated · Avoid setter
Document.domain
Deprecated domain getter/setter for same-origin policy — prefer location.hostname and postMessage.
LegacyCompatibility only
Google ChromeMay still expose · setter discouraged
Legacy / limited
Mozilla FirefoxLegacy support · prefer alternatives
Legacy / limited
Apple SafariDo not rely on setter behavior
Legacy / limited
Microsoft EdgeChromium · avoid new use
Legacy / limited
OperaFollow Chromium deprecation path
Legacy / limited
Internet ExplorerLegacy same-origin domain API
Legacy support
Document.domainAvoid in new code
Bottom line: Recognize document.domain in old scripts. For new work, read hosts with location.hostname and communicate across origins with postMessage — never relax origins with document.domain.
Wrap Up
Conclusion
Document.domain is a deprecated way to read or relax the domain used in same-origin checks. Learn it to maintain old code—then migrate to location.hostname and postMessage.
Think document.domain = document.domain is harmless (MDN)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.domain
Deprecated same-origin domain API — prefer hostname and postMessage.
5
Core concepts
⚠️01
Status
deprecated
MDN
📄02
Type
string
API
🔒03
Setter
unsafe
Security
🌐04
Read
hostname
Prefer
💬05
Talk
postMessage
Modern
❓ Frequently Asked Questions
It gets or sets the domain portion of the document's origin used by the same-origin policy. The getter usually returns the hostname; the setter was historically used to relax subdomain boundaries.
Yes. MDN marks Document.domain deprecated. Avoid it in new code. Prefer location.hostname to read the host, and Window.postMessage for controlled cross-origin communication.
MDN: the getter is not dangerous in the same way as the setter, but location.hostname is simpler and lets you avoid document.domain entirely.
MDN: it undermines same-origin protections, can expose the DOM across subdomains, drops the port from the origin model, and is especially risky on shared hosting.
Use Window.postMessage to send asynchronous messages between origins. That controlled message-passing is much safer than blanket DOM access via document.domain (MDN).
MDN: a SecurityError DOMException for sandboxed documents, no browsing context, null effective domain, or values that are not the current hostname or a parent domain.
Did you know?
Setting document.domain to its current value is still an origin-changing operation (MDN). Pages that “touch” document.domain can become cross-origin relative to sibling pages that never did—another reason the API is a footgun.