How to get the number of the internet host port for the current page in JavaScript?


In this tutorial, we will learn to get the number of internet host port for the current page in JavaScript.

The Port number is a 16-bit unique ID of protocols. The port number ranged from 0-65535. The port number is used to find a process by the server. There are 65535, and each port number is associated with its identity.

So, let us look to get the port number for the current page in JavaScript.

Following are the methods by which we can get the port number of the internet host −

  • Using the location.port Property
  • Using the URL interface

Using the location.port Property

The Location is the interface that accesses the URL of the object. Both Windows and Document Objects can access this interface. The location interface contains a lot of properties, and the port is one of them. The location. port returns the port number of the URL.

Syntax

//get a port number
var port_number= location.port;

In the above syntax, users can see that by using location.port, you can get a port number of a current page. We have to store the value in the variable as per the syntax.

Example 1

In the below given example, we have used the location.port method to get a port number in JavaScript.

<html> <body> <h3> Use <i> location.port </i> to get a port number using JavaScript. </h3> <button id = "btn">get port</button> <p id = "para"> </p> <p><b>Note: </b>If the port number is default (80 for http and 443 for https), most browsers will display nothing.</p> <script> var port_number= location.port; document.getElementById("btn").addEventListener("click",getport); function getport(){ document.getElementById("para").innerHTML="Port Number of the page: "+port_number; } </script> </body> </html>

In the above example, users can see that we have used the location.port method to get the port number of the internet host for the current page in JavaScript. We added a button that will run a function on click. And added our method inside the function to get a port.

Using the URL interface

The URL interface is used to encode the URLs. The interface gives us a lot of properties to access the host, domain or port, etc. It contains a port property that contains the port of the URL.

Viewers can follow the below syntax to use the URL interface to get a port number for the current page in JavaScript.

Syntax

var URL= new URL(window.location.href);
var port_number = URL.port; 

In this syntax, firstly, we have accessed a current URL from a location object and then used the port method on the URL.

Example

In the below given example, we have used the URL interface to get a port number of an internet host for the current page using JavaScript.

<html> <body> <h3> Use <i> URL interface </i> to get a port number using JavaScript. </h3> <button id = "btn">get port</button> <p id = "para"> </p> <p><b>Note: </b>If the port number is default (80 for http and 443 for https), most browsers will display nothing.</p> <script> var URL= new URL(window.location.href); var port_number = URL.port; document.getElementById("btn").addEventListener("click",getport); function getport(){ document.getElementById("para").innerHTML="Port Number of the page: "+port_number; } </script> </body> </html>

In the above example, users can see that we have used the URL interface method to get the port number of the internet host for the current page in JavaScript. This example is the same as the previous one; we have only used the URL interface here.

In this tutorial, we have seen two procedures to get the port number of the internet host for the current page in JavaScript. Both methods are simple and easy to implement as that did not contain any logic.

Updated on: 12-Oct-2022

394 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements