The port
property sets or gets the port
part of the href
attribute value.
port |
Yes | Yes | Yes | Yes | Yes |
Return the port property:
var v = anchorObject.port
Set the port property:
anchorObject.port = number;
Value | Description |
---|---|
number | Set the port number of a URL |
A String representing the port number of the URL.
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:
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: