Open In App

HTML DOM Location port Property

Last Updated : 14 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The HTML DOM Location Port property returns or sets the port number of the current URL. 

Syntax:

  • Get the port property:
  • Set the port property:

Property Value:

  • A String, to be assigned as a port

Return Value:

  • A String represents the port number of a URL.

Example: In this example, we will use the HTML DOM Location Port.

HTML




<!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');
        <!-- Return Location port Property -->
        window.alert("The URL port number of the current page is: "
            + url.port);
    </script>
</body>
</html>


Output:

 

Supported Browsers:

  • Google Chrome 1
  • Edge 12
  • Internet Explorer 3
  • Firefox 1
  • Opera 12.1
  • Safari 1

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads