Javascript Reference - Location hostname Property








The hostname property sets or gets the hostname of a URL.

Browser Support

hostname Yes Yes Yes Yes Yes

Syntax

Return the hostname property.

var v = location.hostname

Set the hostname property.

location.hostname=hostname

Parameter Values

Value Type Description
hostname String Set the hostname of the URL




Return Value

A String type value representing the domain name or the IP address of a URL.

Example

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


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

</body>
</html>

The code above is rendered as follows: