pankaj
pankaj

Reputation: 1733

how to get default port number on which web application is running (or deployed) using javascript

I have a web application which is deployed at some port say 8085, and hostname is sample.something.com. Using window.location.host or window.location.port, the port is coming as blank. How can i retrieve the port number using javascript , can anyone help me out?

Upvotes: 1

Views: 5951

Answers (1)

Anoop
Anoop

Reputation: 23208

If window.location.port is empty that means the application is running on port 80 for http and 443 for https.

As mentioned in a comment, you can use window.location.protocol to check what protocol is used (http or https).

Implementation:

function getPort(){
  if(location.port != ''){
      return location.port;
  }
  else if(location.protocol== 'http'){
     return 80;    
  }
  else if(location.protocol== 'https'){
     return 443;    
  }    
}

Upvotes: 3

Related Questions