The host
property sets or gets the hostname
and port part for the href attribute value.
host |
Yes | Yes | Yes | Yes | Yes |
Return the host property:
var v = anchorObject.host
Set the host property:
anchorObject.host = hostname:port;
Value | Description |
---|---|
hostname:port | Set the URL hostname and port number |
A String representing the domain name or IP address and its port number.
The following code shows how to get the hostname and port number of a link.
<!DOCTYPE html>
<html>
<body>
<p><a id="myAnchor" href="http://www.example.com:1234/test.htm#part1">Example link</a></p>
<!--from w ww . ja va 2s .c o m-->
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myAnchor").host;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to set the hostname and port number.
<!DOCTYPE html>
<html>
<body>
<!--from w ww. ja v a2 s . c o m-->
<p><a id="myAnchor" href="http://www.example.com:1234/test.htm#part1">Example link</a></p>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("myAnchor").host = "www.yourfakeserver.com:1111";
document.getElementById("demo").innerHTML = "changed";
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to change the hostname and port number of a link.
<!DOCTYPE html>
<html>
<body>
<!-- ww w . j av a2 s. c o m-->
<p><a id="myAnchor" href="http://www.example.com:80">Example link</a></p>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("myAnchor").host = "www.yourfakeserver.com:8080";
document.getElementById("demo").innerHTML = "changed";
}
</script>
</body>
</html>
The code above is rendered as follows: