Get protocol, domain, and port from URL

yelo3 picture yelo3 · Aug 4, 2011 · Viewed 352.1k times · Source

I need to extract the full protocol, domain, and port from a given URL. For example:

https://localhost:8181/ContactUs-1.0/contact?lang=it&report_type=consumer
>>>
https://localhost:8181

Answer

Shef picture Shef · Aug 4, 2011
var full = location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '');