Search code examples
javascripturldnsprotocolsport

Get protocol, domain, and port from URL


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

Solution

  • first get the current address

    var url = window.location.href
    

    Then just parse that string

    var arr = url.split("/");
    

    your url is:

    var result = arr[0] + "//" + arr[2]