Open In App

Web API URL.port Property

Last Updated : 12 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The Web API URL.port property is used to get USVString containing the port number of the URL.

Syntax:

var str = URL.port

Return Value: This property returns a USVString containing the port number of the URL.

Example 1:

HTML
<!DOCTYPE html>
<html>

<body>
    <h1>GeeksforGeeks</h1>

    <button onclick="get()">
        Click on Me!
    </button>

    <script type="text/javascript">
        function get() {
            var url = new URL(
'https://www.geeksforgeeks.org:123/GeeksforGeeks');

            console.log("port of current URL is :",
                url.port);
        }
    </script>
</body>

</html>

Output:

Example 2:

HTML
<!DOCTYPE html>
<html>

<body>
    <h1>GeeksforGeeks</h1>

    <div id="abc"></div>

    <br><br>
    <button onclick="get()">
        Click on Me!
    </button>

    <script type="text/javascript">
        function get() {
            var url = new URL(
'https://www.geeksforgeeks.org:123/GeeksforGeeks');

            a = document.getElementById("abc");

            a.innerHTML = "port of current URL is : " 
                + url.port;
        }
    </script>
</body>

</html>

Output:

Supported Browsers:

  • Safari 10
  • Opera 19
  • Chrome 32
  • Edge 13
  • Firefox 22

Similar Reads