Return the port number of the current URL:
var x = "Port: " + location.port;
If the port number is default (80 for http and 443 for https), most browsers will display 0 or nothing.
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "The URL port number of the current page is: " + window.location.port; </script>/*ww w.j a v a2s . c om*/ </body> </html>
The port property sets or gets the port number the server uses for a URL.
If the port number is not specified in the URL (or if it is the default port - like 80, or 443), some browsers will display 0 or nothing.
Property Values
Value | Type | Description |
---|---|---|
port | String | Specifies the port number of the URL |
The port property returns a String, representing the port number of a URL.