Javascript Reference - HTML DOM Anchor port Property








The port property sets or gets the port part of the href attribute value.

Browser Support

port Yes Yes Yes Yes Yes

Syntax

Return the port property:

var v = anchorObject.port 

Set the port property:

anchorObject.port = number;

Property Values

Value Description
number Set the port number of a URL




Return Value

A String representing the port number of the URL.

Example

The following code shows how to Return the port number of a link:


<!DOCTYPE html>
<html>
<body>
<!--from   w  ww .  j  a  va  2s .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() {
    var x = document.getElementById("myAnchor").port;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the port number of a link.


<!DOCTYPE html>
<html>
<body>
<!--from  w  w w  .j a  va2s .  com-->
<p><a id="myAnchor" href="http://www.example.com:443/test.htm#part2">Example link</a></p>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("myAnchor").port = "80";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: