Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

In browser JavaScript, read window.location.href to get the current page’s complete URL as a string. It includes the scheme, host, path, query string, and fragment. Use window.location.pathname, window.location.search, or another Location property when you need only one part.

Get the full current URL

Run this in a browser page’s JavaScript context:

const currentUrl = window.location.href;
console.log(currentUrl);

window.location is the Location object for the window, and its href property gives you the full current URL. document.location.href accesses the same document location, but window.location.href is the direct, conventional choice when you mean the URL of the current browser window.

The result is a string, so you can store it, log it, or pass it to code that needs the complete address. This reads the URL; it does not change where the browser is navigating.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the URL part you actually need

The full URL is often more information than a task requires. The Location object exposes separate properties for its components:

What you need JavaScript What it returns
Complete URL window.location.href The full URL, including path, query string, and fragment.
Origin window.location.origin The scheme, hostname, and port.
Hostname and optional port window.location.host The host portion, including a port when present.
Path window.location.pathname The path, without the query string or fragment.
Query string window.location.search The query portion, including its leading ?.
Fragment window.location.hash The fragment portion, including its leading #.

For example, if the address is https://example.com/products?campaign=spring#details, href is the whole address, pathname is /products, search is ?campaign=spring, and hash is #details. Use origin or host when you need to identify the site rather than its particular page. MDN’s Location reference documents these properties and their distinct values.

Read query parameters safely with URLSearchParams

For a specific query parameter, parse the current address with the URL API and read it through searchParams. This avoids treating the entire query string as a single value:

const url = new URL(window.location.href);
const campaign = url.searchParams.get("campaign");

console.log(campaign);

If the current address contains ?campaign=spring, the value returned for campaign is spring. If that parameter is not present, get() returns null; account for that possibility before using the value:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const url = new URL(window.location.href);
const campaign = url.searchParams.get("campaign");

if (campaign !== null) {
  console.log(`Campaign: ${campaign}`);
} else {
  console.log("No campaign parameter in this URL.");
}

Use the URL API when you want structured access to the current URL’s parts as well as its query parameters. MDN’s URL reference and URL API guide describe the constructor, the URL components, and the searchParams interface.

Read the URL without navigating, or change it intentionally

Reading window.location.href only retrieves the address. Assigning a value to it has a different effect: it navigates the browser to the supplied URL.

// Read the current address; this does not navigate.
const currentUrl = window.location.href;

// Assigning a URL navigates the browser.
// window.location.href = "https://example.com/";

Another navigation method is window.location.replace(url). Unlike ordinary navigation, replace() does not preserve the current page in session history for the browser’s Back navigation. Keep that distinction in mind when choosing between reading a URL, assigning to href, and replacing the current history entry. MDN explains this behavior in its Location: href property reference.

Handle iframe URLs and origin restrictions

A script running in a page cannot freely read the complete URL of an iframe from a different origin. The browser’s same-origin policy restricts that access; a cross-origin Location.href is write-only to the accessing script. If your pages need to communicate across origins, use window.postMessage rather than trying to read the other document’s URL directly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This restriction is about access between documents from different origins. Reading window.location.href for the current page is the direct approach described above. For the security rule and cross-document context, see MDN’s same-origin policy guide.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common mistakes

  • You only see a path. pathname intentionally excludes the query and fragment. Use href for the complete address.
  • Your query value includes punctuation or more than one parameter. location.search returns the whole query string with its leading ?. Parse the URL with new URL(window.location.href) and retrieve the particular value using searchParams.get("name").
  • The value is null. URLSearchParams.get() returns null when the requested parameter is absent. Check for that case before relying on the value.
  • Your code changes pages unexpectedly. Check whether the code assigns to location.href or calls location.replace(). Those are navigation operations, not URL-reading operations.
  • You cannot read an iframe’s URL. If the frame is from another origin, the same-origin policy limits access. Use postMessage for communication between the documents.

Or skip the browser setup

If your goal is to capture a page image rather than read its address inside page JavaScript, you can supply a URL directly to ScreenshotNeo, a website screenshot API and MCP server for developers. Its API captures a supplied URL; it does not discover the current URL from the browser tab for you. Use the actual page URL you want to capture.

For example, this cURL request saves a WebP screenshot of the specified page. See the ScreenshotNeo API documentation for request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

The same request from Python:

import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
open("shot.webp", "wb").write(r.content)

Or call the endpoint from Node.js:

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

ScreenshotNeo accepts PNG, JPEG, or WebP output, or can return a PDF. Before capture, it accepts consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each of those steps can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers indicate the page verdict and billing status. An MCP server exposes take_screenshot, get_page_info, and capture_pdf for Claude, Cursor, and other MCP clients.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The free plan includes 1,000 screenshots per month with no card required; paid plans start at $5 for 3,000 screenshots. Sign up for ScreenshotNeo’s free plan.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.