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