This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

window.location.hostname

Summary

The hostname property contains the hostname the current document was served from, excluding protocol, port, and other information.

Property of apis/locationapis/location

Syntax

Note: This property is read-only.

var result = window.location.hostname;

Return Value

Returns an object of type StringString

The hostname the current document was served from.

For example, http://example.org/ would return the hostname example.org.

Examples

The following example assumes your document has a div element with id 'hostDiv’, like this.

// Get the hostname from window.location
var hostnm = window.location.hostname;

// Get a div element with id 'hostDiv'
var container = document.getElementById('hostDiv');

// Fill in the div element with the hostname
container.innerHTML = hostnm;

Related specifications

Window Object 1.0
W3C Working Draft