如何使用javascript获取正在运行(或部署)Web应用程序的默认端口号 [英] how to get default port number on which web application is running (or deployed) using javascript
问题描述
我有一个Web应用程序,该应用程序部署在某个端口(例如8085)上,主机名是sample.something.com.使用window.location.host或window.location.port,端口变为空白. 我如何使用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?
推荐答案
如果window.location.port
为空,则表示该应用程序在http的端口80上运行,而在https的端口443上运行.
If window.location.port
is empty that means the application is running on port 80 for http and 443 for https.
如评论中所述,您可以使用window.location.protocol
来检查所使用的协议(http或https).
As mentioned in a comment, you can use window.location.protocol
to check what protocol is used (http or https).
实施:
function getPort(){
if(location.port != ''){
return location.port;
}
else if(location.protocol== 'http'){
return 80;
}
else if(location.protocol== 'https'){
return 443;
}
}
这篇关于如何使用javascript获取正在运行(或部署)Web应用程序的默认端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!