The hostname
property sets or gets the hostname of a URL.
hostname |
Yes | Yes | Yes | Yes | Yes |
Return the hostname property.
var v = location.hostname
Set the hostname property.
location.hostname=hostname
Value | Type | Description |
---|---|---|
hostname | String | Set the hostname of the URL |
A String type value representing the domain name or the IP address of a URL.
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: