Javascript Reference - Location host Property








The host property sets or gets the hostname and port of a URL.

Browser Support

host Yes Yes Yes Yes Yes

Syntax

Return the host property.

var v = location.host

Set the host property.

location.host=hostname:port

Parameter Values

Value Type Description
hostname:port String Set the hostname and port number of the URL




Return Value

A String type value representing the domain name and port number, or the IP address of a URL.

Example

The following code shows how to get the hostname and port of the current URL.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!--  w  w  w .j  a va  2 s. c o  m-->
<script>
function myFunction() {
    var x = location.host;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: