如何使用jQuery从URL获取端口号

How to get the port number from the URL using jQuery?

本文关键字:获取 口号 URL 何使用 jQuery      更新时间:2023-09-26

这是URL:

http:// localhost:8888/index.html. 

如何使用jQuery从这个URL获得端口号?

location对象中可用:

location.port

请注意,当URL中没有端口时,location.port将返回一个空字符串。

如果您需要在使用隐式默认端口时获取端口,请尝试:

var port = location.port || (location.protocol === 'https:' ? '443' : '80');

这对于通过httphttps协议提供的页面应该足够了。

不需要jQuery。

window.document.location.port