Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jQuery从URL获取端口号?_Javascript_Jquery_Url_Port - Fatal编程技术网

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

Javascript 如何使用jQuery从URL获取端口号?,javascript,jquery,url,port,Javascript,Jquery,Url,Port,这是URL: http:// localhost:8888/index.html. 如何使用jQuery从此URL获取端口号?对象中提供了该端口号: 请注意,当URL中不存在端口时,location.port将返回空字符串 如果在使用隐式默认端口时仍需要获取端口,请尝试: var port = location.port || (location.protocol === 'https:' ? '443' : '80'); 这应该足以满足通过http和https协议提供的页面。您不需要jQ

这是URL:

http:// localhost:8888/index.html. 
如何使用jQuery从此URL获取端口号?

对象中提供了该端口号:

请注意,当URL中不存在端口时,
location.port
将返回空字符串

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

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

这应该足以满足通过
http
https
协议提供的页面。

您不需要jQuery

window.document.location.port

你忘了发布你尝试的jQuery,但没有成功。重复MDN页面上提到的location.port吗?@j08691很好,我已经替换了链接<代码>窗口。位置从
URLUtils
接口继承它。
window.document.location.port