【问题标题】:How to get the port number from the URL using jQuery?如何使用 jQuery 从 URL 中获取端口号?
【发布时间】:2014-07-07 11:56:51
【问题描述】:

这是网址:

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

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

【问题讨论】:

标签: javascript jquery url port


【解决方案1】:

它在location 对象中可用:

location.port

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

如果在使用隐式默认端口的情况下仍需要获取端口,请尝试:

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

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

【讨论】:

  • MDN 页面上提到的 location.port 在哪里?
  • @j08691 好点,我已经替换了链接。 window.location 继承自 URLUtils 接口。
【解决方案2】:

你不需要 jQuery。

window.document.location.port

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    相关资源
    最近更新 更多