English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Location port attribute

JavaScript Location  Object

portProperty sets or returns the port of the URL.

Note:If the port number is not specified in the URL or it is the default port of the scheme (for example80 or443),then the port property will return an empty string.

Syntax:

Return Port Property:

location.port

Set Port Property:

location.port = port
document.write("The port number is: " + location.port);
Test See‹/›

Browser Compatibility

All browsers fully support the port property:

Property
portYesYesYesYesYes

Property Value

ValueDescription
portA string specifying the port number of the URL

Technical Details

Return Value:A string representing the port number of the URL

Related References

Location Reference:location.href property

Location Reference:location.host property

Location Reference:location.origin property

Location Reference:location.protocol property

JavaScript Location  Object