有没有一种简单的方法可以从完整的URL开始:
document.location.href = "http://aaa.bbb.ccc.com/asdf/asdf/sadf.aspx?blah"
并且只提取主机部分:
aaa.bbb.ccc.com
肯定有一个JavaScript函数可以可靠地做到这一点,但我找不到它.
有没有一种简单的方法可以从完整的URL开始:
document.location.href = "http://aaa.bbb.ccc.com/asdf/asdf/sadf.aspx?blah"
并且只提取主机部分:
aaa.bbb.ccc.com
肯定有一个JavaScript函数可以可靠地做到这一点,但我找不到它.
假设你有一个地址为http://sub.domain.com/virtualPath/page.htm
的页面.使用以下页面代码来获得这些结果:
window.location.host
:你会得到sub.domain.com:8080
或sub.domain.com:80
分window.location.hostname
:你会得到sub.domain.com
分window.location.protocol
:你会得到http:
分window.location.port
:你会得到8080
或80
分window.location.pathname
:你会得到/virtualPath
分window.location.origin
:你会得到http://sub.domain.com
*Update: about the .origin
*****正如ref个州所说,window.location.origin
的浏览器兼容性尚不清楚.我在chrome中判断过,如果端口不是80,它返回http://sub.domain.com:port
,如果端口是80,它返回http://sub.domain.com
.
Special thanks to @torazaburo for mentioning that to me.个