port property
Sets or retrieves the port number associated with a URL.
Syntax
| JavaScript | |
|---|
Property values
Type: String
the port number associated with the URL. See remarks.
Remarks
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.
Examples
This example function returns the port property of two a elements.
<!DOCTYPE html> <html> <head> <title>Port</title> </head> <body> <p>Click a link:</p> <a href="ftp://www.microsoft.com" onclick="getPort(this);">ftp</a> <a href="http://www.microsoft.com" onclick="getPort(this);">http</a> <script> function getPort(evt) { console.log("FTP: " + evt.port + "\n" + "HTTP: " + evt.port); } </script> </body> </html>
See also
Show: