This page is Almost Ready

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

port

Summary

Sets or retrieves the port number associated with a URL.

Property of dom/Locationdom/Location

Syntax

var port = location.port;
location.port = port;

Return Value

Returns an object of type StringString

The port of the URL.

Examples

This example function returns the port property of two a elements.

<script>
function getPort()
{
    console.log("FTP: " + document.getElementById("ftp").port + "\n" + "HTTP: " + document.getElementById("http").port);
}
</script>

<a href="ftp://www.microsoft.com" onclick="getPort();" id="ftp">ftp</a>
<a href="http://www.microsoft.com" onclick="getPort();" id="http">http</a>

Notes

The port will resolve based on the default port for the protocol set in the HREF attribute: 21 for FTP, 80 for HTTP, and so forth. Proprietary protocols that do not require a port return 0 or an empty string. location.port returns an empty string when read in a page reached by the http protocol.

Attributions