Coder Perfect

From the URL, get the protocol, domain, and port.


From a given URL, I need to extract the whole protocol, domain, and port. Consider the following scenario:


Asked by yelo3

Solution #1

const full = location.protocol + '//' +;

Answered by Shef

Solution #2

None of these responses appear to really satisfy the question, which requires an arbitrary url rather than the current page’s url.

The URL API can be used (not supported by IE11, but available everywhere else).

This makes it simple to access search parameters. It can also be utilized in a Web Worker because it is not dependent on the DOM.

const url = new URL('');

If you need it to work on previous browsers as well, this is the way to go.

//  Create an anchor element (note: no need to append this element to the document)
const url = document.createElement('a');
//  Set href to any path
url.setAttribute('href', '');

The built-in parser in your browser has already completed its task. Now you can just grab the bits you require (this applies to both approaches above):

//  Get any piece of the url you're interested in
url.hostname;  //  ''
url.port;      //  12345;    //  '?startIndex=1&pageSize=10'
url.pathname;  //  '/blog/foo/bar'
url.protocol;  //  'http:'

Because ‘?startIndex=1&pageSize=10’ isn’t very useful on its own, you’ll probably want to break up the search url params as well.

You can just use the searchParams getters if you used Method 1 (URL API) above:

url.searchParams.get('startIndex');  // '1'

Alternatively, to obtain all parameters, type:

function searchParamsToObj(searchParams) {
  const paramsMap = Array
    .reduce((params, [key, val]) => params.set(key, val), new Map());
  return Object.fromEntries(paramsMap);
// -> { startIndex: '1', pageSize: '10' }

You can use something like this if you utilized Method 2 (the old way):

// Simple object output (note: does NOT preserve duplicate keys).
var params =; // remove '?' prefix
    .reduce((accum, keyval) => {
        const [key, val] = keyval.split('=');
        accum[key] = val;
        return accum;
    }, {});
// -> { startIndex: '1', pageSize: '10' }

Answered by David Calhoun

Solution #3

Get the current address first.

var url = window.location.href

Then simply parse the string.

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

your url is:

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

Hope this helps

Answered by wezzy

Solution #4

For whatever reason, all of the responses are excessive. It’s as simple as that:


More details can be found here:

Answered by Pijusn

Solution #5

As previously said, window.location.origin is still not completely supported, but rather than utilizing it or creating a new variable to use, I prefer to check for it and set it if it isn’t set.

For example;

if (!window.location.origin) {
  window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');

This is something I wrote about a few months ago. window.location.origi has been fixed.

Answered by Toby

Post is based on