JavaScript document.URL to Read and Parse the Current Address

Read document.URL to get the current document address as a read-only string. To access the path, query, or fragment separately, parse it with new URL(document.URL).

Read the complete document address

Read document.URL without parentheses. If the address has a query and fragment, they are included. To navigate to another address, use location.assign() or assign a value to location.href. DOM Standard · Document.URL URL properties of the Location object

console.log(document.URL);
console.log(location.href);

Separate the path, query parameters, and fragment

pathname gives the path, search gives the query string with its leading ?, and hash gives the fragment with its leading #. The corresponding property is empty when the query or fragment is absent. The sample uses a fixed URL so it gives the same result on any page. URL Standard · URL API

const current = new URL('https://example.com/guide?lang=ko&lang=en#example');
console.log(current.pathname);                // '/guide'
console.log(current.search);                  // '?lang=ko&lang=en'
console.log(current.hash);                    // '#example'
console.log(current.searchParams.get('lang')); // 'ko'
console.log(current.searchParams.getAll('lang')); // ['ko', 'en']
console.log(current.searchParams.get('page')); // null

const empty = new URL('https://example.com/guide?lang=');
console.log(empty.searchParams.get('lang')); // ''

searchParams.get('lang') gives the first value. It returns null if the parameter is missing and an empty string for ?lang=. Use getAll() to read every value with the same name.

To resolve a relative path within a document, pass a base as in new URL('intro.html', document.baseURI). A <base href> element can cause baseURI to differ from document.URL. HTML Standard · Document base URL

Observe a fragment change in a complete example

Save the code below as document-url.html in UTF-8 and open it in a browser. Select the link to the section below. The address gains #details, and the hashchange handler updates the output. You can also read the values again with the button.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Inspect the current document URL</title>
</head>
<body>
<p><a href="#details">Go to the section below</a></p>
<button id="refresh" type="button">Read the URL again</button>
<pre id="result" aria-live="polite"></pre>
<p id="details">Section below</p>
<script>
const result = document.querySelector('#result');
const missing = "None";

function showURL() {
  const current = new URL(document.URL);
  const lang = current.searchParams.get('lang');
  result.textContent = [
    `Full URL: ${document.URL}`,
    `Path: ${current.pathname}`,
    `Query: ${current.search || missing}`,
    `Fragment: ${current.hash || missing}`,
    `lang: ${lang === null ? missing : JSON.stringify(lang)}`
  ].join('\n');
}

document.querySelector('#refresh').addEventListener('click', showURL);
window.addEventListener('hashchange', showURL);
showURL();
</script>
</body>
</html>

A file opened locally starts with file:. Insert ?lang=ko before #details in the address and open it again to see "ko" for lang. With ?lang=, it shows "". Remove the parameter to see the missing-value label.