Nuestros voluntarios aún no han traducido este artículo al Español. ¡Únete a nosotros y ayúdanos a traducirlo!
También puedes leer el artículo en English (US).

The domain property of the Document interface gets/sets the domain portion of the origin of the current document, as used by the same origin policy.

If this property is successfully set, the port part of the origin is also set to null.

Syntax

var domainString = document.domain;
document.domain = string;

Value

The domain portion of the current document's origin.

Exceptions

SecurityError
An attempt has been made to set domain under one of the following conditions:
  • The document is inside a sandboxed <iframe>
  • The document has no browsing context
  • The document's effective domain is null
  • The given value is not equal to the document's effective domain (or it is not a registerable domain suffix of it)
  • The document-domain Feature-Policy is enabled

Examples

Getting the domain

For the URI http://developer.mozilla.org/en-US/docs/Web, this example sets currentDomain to the string "developer.mozilla.org".

var currentDomain = document.domain;

Closing a window

If a document, such as www.example.xxx/good.html, has the domain of "www.example.xxx", this example attempts to close the window.

var badDomain = "www.example.xxx";

if (document.domain == badDomain) {
  // Just an example: window.close() sometimes has no effect
  window.close();
}

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'Document.domain' in that specification.
Living Standard  

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
domainChrome Full support 1Edge Full support 12Firefox Full support Yes
Notes
Full support Yes
Notes
Notes From Firefox 62, if the domain cannot be identified, domain returns an empty string instead of null. See bug 819475.
IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support Yes
Notes
Full support Yes
Notes
Notes From Firefox 62, if the domain cannot be identified, domain returns an empty string instead of null. See bug 819475.
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

Firefox notes

In Firefox, this property used to return null if the domain of the document could not be identified, although this has now changed as of Firefox 62 — see the discussion in bug 819475.

Mozilla will let you set this property to a superdomain of the current value, constrained by its base domain. For example, on developer.mozilla.org it is possible to set it to "mozilla.org" but not "mozilla.com" or "org".

Mozilla distinguishes a document.domain property that has never been set from one explicitly set to the same domain as the document's URL, even though the property returns the same value in both cases. One document is allowed to access another if they have both set document.domain to the same value, indicating their intent to cooperate, or neither has set document.domain and the domains in the URLs are the same (implementation). Were it not for this special policy, every site would be subject to XSS from its subdomains (for example, https://bugzilla.mozilla.org could be attacked by bug attachments on https://bug*.bugzilla.mozilla.org).

See also