Javascript Reference - Location port Property








The port property sets or gets the port number for a URL.

Browser Support

port Yes Yes Yes Yes Yes

Syntax

Return the port property.

var v = location.port

Set the port property.

location.port=port

Parameter Values

Value Type Description
port String Set the port number of the URL




Return Value

A String type value representing the port number of a URL.

Example

The following code shows how to get the port number of the current URL.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!-- w ww .jav a 2s.  co  m-->
    var x = "Port: " + location.port;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: