English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Location host attribute

JavaScript Location  Object

hostSets or returns the hostname and port of the URL.

Note:If the host attribute is not specified in the URL, the port number will not be returned.

Syntax:

Return the host attribute:

location.host

Set the host attribute:

location.host = hostname:port
var x = location.host;
document.querySelector("#output").innerHTML = x;
Test and See‹/›

Browser compatibility

All browsers fully support the host attribute:

Attribute
hostIsIsIsIsIs

Attribute value

ValueDescription
hostname:portA string that specifies the hostname and port of the URL

Technical details

Return value:A string representing the domain name, port, or IP address of the URL

More examples

This example displays all location attributes:

var txt = "";
txt += "<p>Host: " + location.host + "</p>";
txt += "<p>Hostname: " + location.hostname + "</p>";
txt += "<p>Href: " + location.href + "</p>";
txt += "<p>Origin: " + location.origin + "</p>";
txt += "<p>Pathname: " + location.pathname + "</p>";
txt += "<p>Protocol: " + location.protocol + "</p>";
txt += "<p>Search: " + location.search + "</p>";
document.write(txt);
Test and See‹/›

Related References

Location Reference:location.href property

Location Reference:location.hostname property

Location Reference:location.pathname property

Location Reference:location.protocol property

JavaScript Location  Object