【发布时间】:2014-07-07 11:56:51
【问题描述】:
这是网址:
http:// localhost:8888/index.html.
如何使用 jQuery 从这个 URL 获取端口号?
【问题讨论】:
-
您忘记发布您尝试过但不起作用的 jQuery。
标签: javascript jquery url port
这是网址:
http:// localhost:8888/index.html.
如何使用 jQuery 从这个 URL 获取端口号?
【问题讨论】:
标签: javascript jquery url port
它在location 对象中可用:
location.port
请注意,当 URL 中没有端口时,location.port 将返回一个空字符串。
如果在使用隐式默认端口的情况下仍需要获取端口,请尝试:
var port = location.port || (location.protocol === 'https:' ? '443' : '80');
这对于通过http 和https 协议提供的页面应该足够了。
【讨论】:
window.location 继承自 URLUtils 接口。
你不需要 jQuery。
window.document.location.port
【讨论】: