Javascript Reference - HTML DOM Anchor host Property








The host property sets or gets the hostname and port part for the href attribute value.

Browser Support

host Yes Yes Yes Yes Yes

Syntax

Return the host property:

var v = anchorObject.host 

Set the host property:

anchorObject.host = hostname:port;

Property Values

Value Description
hostname:port Set the URL hostname and port number




Return Value

A String representing the domain name or IP address and its port number.

Example

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:





Example 2

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:

Example 3

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: