Read the current URL, domain, and path with JavaScript location

Use window.location.href for the current address, window.location.hostname for the hostname, and window.location.pathname for the path. The Location object is tied to the current document’s URL. Use new URL(address) to parse a separate address.

Read the parts of the current URL

The table breaks down the example https://example.com:8080/path/page.html?lang=ko&tag=js&tag=dom&q=hello+world#part. The difference between host and hostname is the port. When it is omitted or set to the HTTPS default of 443, port is empty and host omits it. See HTML Living Standard for the property definitions.

Property Example value Meaning
href https://example.com:8080/path/page.html?lang=ko&tag=js&tag=dom&q=hello+world#part Full URL
origin https://example.com:8080 HTTP(S) scheme, hostname, and port
protocol https: Scheme with a trailing colon
host example.com:8080 Hostname and non-default port
hostname example.com Hostname without the port
port 8080 Non-default port number
pathname /path/page.html Path without the query or fragment
search ?lang=ko&tag=js&tag=dom&q=hello+world Query including the question mark
hash #part Fragment including the hash sign

Parse addresses and query parameters

Run the snippet in the browser console. The first two lines log the active document’s address and path. The rest analyze the sample address. Parsing with new URL() makes no network request and leaves the current page in place.

const current = window.location;
console.log(current.href);
console.log(current.pathname);

const sample = new URL('https://example.com:8080/path/page.html?lang=ko&tag=js&tag=dom&q=hello+world#part');
console.log(sample.host);
console.log(sample.hostname);
console.log(sample.searchParams.get('lang'));   // ko
console.log(sample.searchParams.get('tag'));    // js
console.log(sample.searchParams.getAll('tag')); // ['js', 'dom']
console.log(sample.searchParams.get('q'));      // hello world

search is the query string, including ?. searchParams.get('tag') returns the first value, js. getAll('tag') returns ['js', 'dom'] in order. For a missing key, get() returns null and getAll() returns an empty array. A present tag= gives an empty string. A + decodes to a space in the query, so q becomes hello world. See URL Standard.

Complete example: compare the current URL with an entered URL

Save the HTML in UTF-8 as location-example.html and open it in a browser. Parse URL reads the absolute address in the field. Current page URL copies and parses the address of the open document. Reset restores the sample.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Inspect URL components</title>
</head>
<body>
<label for="address">Absolute URL to inspect</label>
<input id="address" type="url" size="60" value="https://example.com:8080/path/page.html?lang=ko&amp;tag=js&amp;tag=dom&amp;q=hello+world#part">
<button id="parse" type="button">Parse URL</button>
<button id="current" type="button">Current page URL</button>
<button id="reset" type="button">Reset</button>
<pre id="output" aria-live="polite"></pre>
<script>
const address = document.querySelector('#address');
const output = document.querySelector('#output');

function show(value) {
  try {
    const url = new URL(value);
    output.textContent = [
      `href: ${url.href}`,
      `origin: ${url.origin}`,
      `protocol: ${url.protocol}`,
      `host: ${url.host}`,
      `hostname: ${url.hostname}`,
      `port: ${url.port || "(none)"}`,
      `pathname: ${url.pathname}`,
      `search: ${url.search || "(none)"}`,
      `hash: ${url.hash || "(none)"}`,
      `lang: ${url.searchParams.get('lang') ?? "(none)"}`,
      `tag: ${JSON.stringify(url.searchParams.getAll('tag'))}`,
      `q: ${url.searchParams.get('q') ?? "(none)"}`,
    ].join('\n');
  } catch (error) {
    output.textContent = "Enter a valid absolute URL.";
  }
}

document.querySelector('#parse').addEventListener('click', () => show(address.value));
document.querySelector('#current').addEventListener('click', () => {
  address.value = window.location.href;
  show(address.value);
});
document.querySelector('#reset').addEventListener('click', () => {
  address.value = 'https://example.com:8080/path/page.html?lang=ko&tag=js&tag=dom&q=hello+world#part';
  show(address.value);
});
show(address.value);
</script>
</body>
</html>

In the default output, host includes :8080 and hostname omits it. The tag array shows both values and q contains a space. Entering only /path/page.html shows an error because it needs a base. To resolve a relative path in your code, pass a base with new URL(relativePath, baseURL). Opened from disk, the current address uses file: and differs from an HTTP(S) URL.

Read an address or navigate to it

Assigning to location.href or calling location.assign(url) navigates. location.replace(url) navigates and replaces the current history entry, so Back skips the replaced page. This example only displays properties. Before using an address for navigation, check that its scheme and origin are allowed by your project. See HTML Living Standard.