suppose that you have a page with this address:
http://sub.domain.com/page.htm
. use the following in page code to achive those results:window.location.host
: you’ll getsub.domain.com:8080
orsub.domain.com:80
window.location.hostname
: you’ll getsub.domain.com
window.location.protocol
: you’ll gethttp:
window.location.port
: you’ll get8080
or80
window.location.origin
: you’ll gethttp://sub.domain.com
*
Update: about the .origin
* As the ref states, browser compatibility for
window.location.origin
is not clear. I’ve checked it in chrome and it returned http://sub.domain.com:port
.出處
http://stackoverflow.com/questions/1368264/get-host-name-in-javascript
http://stackoverflow.com/questions/1368264/get-host-name-in-javascript