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

喜夏-厌秋 提交于 2019-12-25 02:46:55

问题


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?


回答1:


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;    
  }    
}


来源:https://stackoverflow.com/questions/12601001/how-to-get-default-port-number-on-which-web-application-is-running-or-deployed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!