URL: host property
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since November 2015.
Note: This feature is available inWeb Workers.
Thehost property of theURL interface is a string containing the host, which is thehostname, and then, if theport of the URL is nonempty, a":", followed by theport of the URL. If the URL does not have ahostname, this property contains an empty string,"".
This property can be set to change both the hostname and the port of the URL. If the URL's scheme is nothierarchical (which the URL standard calls "special schemes"), then it has no concept of a host and setting this property has no effect.
Note:If the given value for thehost setter lacks aport, the URL'sport will not change. This can be unexpected as thehost getter does return a URL-port string, so one might have assumed the setter to always "reset" both.
In this article
Value
A string.
Examples
let url = new URL("https://developer.mozilla.org/en-US/docs/Web/API/URL/host");console.log(url.host); // "developer.mozilla.org"url = new URL("https://developer.mozilla.org:443/en-US/docs/Web/API/URL/host");console.log(url.host); // "developer.mozilla.org"// The port number is not included because 443 is the scheme's default porturl = new URL("https://developer.mozilla.org:4097/en-US/docs/Web/API/URL/host");console.log(url.host); // "developer.mozilla.org:4097"Specifications
| Specification |
|---|
| URL> # dom-url-host> |
Browser compatibility
See also
- The
URLinterface it belongs to.