HTML | DOM Location port Property
The HTML DOM Location Port property returns or sets the port number of the current URL.
Syntax:
- Get the port property:
location.port
location.port = 8080
Property Value:
Return Value:
Examples:
<!DOCTYPE html> <html> <body> <h2>GeeksforGeeks</h2> <p id="demo"></p> <p><b>Note: </b>If the port number is default (80 for http and 443 for https), most browsers will display 0 or nothing. </p> <script> const url = document.createElement('a'); url.setAttribute('href', 'http://example.com:12345/gfg'); <!-- accessing "url.port". --> window.alert("The URL port number of the current page is: " + url.port); </script> </body> </html> |
chevron_right
filter_none
Output :

Supported Browsers:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- HTML | DOM Anchor port Property
- HTML | DOM Area port Property
- HTML | DOM Location host Property
- HTML | DOM Location origin Property
- HTML | DOM Location protocol Property
- HTML | DOM Location pathname Property
- HTML | DOM Location hash Property
- HTML | DOM Location hostname Property
- HTML | DOM Location href Property
- HTML | DOM KeyboardEvent location Property
- HTML | DOM Location Search Property
- HTML | DOM Location reload() Method
- HTML | Location assign( ) Method
- HTML | DOM Location replace() Method
- Node | URL.port API
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

