Document.domain

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

const domainString = document.domain
document.domain = domainString

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:

Examples

Getting the domain

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

const 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.

const 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

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox 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 YesFirefox 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 Full support Yes

Legend

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

See also