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:- 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
- The document is inside a sandboxed
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
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
domain | Chrome Full support 1 | Edge Full support 12 | Firefox
Full support
Yes
| IE ? | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android
Full support
Yes
| Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.