JavaScript WorkerLocation pathname Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

WorkerLocation.pathname returns the pathname part of the worker’s location as a string—the path only (no scheme, host, query, or hash). Per MDN, on a docs URL it can be "/en-US/docs/Web".

Read the notes below before you rely on this property. Prefer feature detection when support is limited.

Syntax

Use location.pathname. Start with this line.

JavaScript
location.pathname
CallWhat it does
location.pathnamePrimary signature.

Built into Web Workers. Read self.location.pathname (or location.pathname) inside a worker — no import required.

Library declarations
JavaScript
location.pathname

Parameters

ParameterDescription
Worker contextRequired. self.location is a WorkerLocation
Writable?Optional. Read-only

Return Value

Return ValueDescription
*Path string (often starts with /)

JavaScript Examples

Example 1 — Read location.pathname

Post the worker pathname string (MDN idea).

JavaScript
// Inside a Web Worker
const result = self.location.pathname;
// e.g. "/en-US/docs/Web"
self.postMessage(result);
Try It Yourself

How It Works

  1. Worker context. Inside the worker, self.location is a WorkerLocation.
  2. Read pathname. location.pathname returns only the path segment.
  3. MDN shape. On a docs URL, you may get "/en-US/docs/Web".
  4. Blob labs differ. Try-it demos use blob workers, so the live path often looks like a UUID path.

Example 2 — pathname vs href

See that pathname drops scheme/host while href keeps the full URL.

JavaScript
self.postMessage({
  pathname: location.pathname,
  href: location.href
});
Try It Yourself

How It Works

  1. Read both. pathname and href come from the same location.
  2. href is complete. It includes protocol, host, path, and more.
  3. pathname is trimmed. No scheme, host, query, or fragment.
  4. Pick the right one. Need only the path? Use pathname. Need the script URL? Use href.

Example 3 — Blob Worker Path

Inspect pathname, protocol, and href together for a blob worker.

JavaScript
self.postMessage({
  pathname: location.pathname,
  protocol: location.protocol,
  href: location.href
});
Try It Yourself

How It Works

  1. Blob URL. Object-URL workers use a blob: script address.
  2. Protocol clue. location.protocol is typically "blob:".
  3. Path shape. The pathname is often a UUID-style segment, not a docs path.
  4. Debug tip. Pair pathname with href so you still see the full blob URL.

Browser Support

WorkerLocation.pathname is marked Baseline Widely available on MDN (since July 2015). It is only available in Web Workers — not in Node.js, Bun, or Deno.

✓ Baseline · Widely available

WorkerLocation.pathname

Supported across modern browsers with Web Workers. Read location.pathname inside the worker.

98% Browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
WorkerLocation.pathname Excellent

Bottom line: Browser / Web Workers only — not a Node.js API. Expect MDN-style paths on http(s) scripts; blob demos usually print a UUID-style path.

Frequently Asked Questions

A string: the pathname part of the worker’s location. Access it inside a worker as self.location.pathname or location.pathname.
No. MDN marks WorkerLocation.pathname as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is only available in Web Workers.
href is the full serialized worker URL. pathname is only the path segment—no scheme, host, query, or hash.
Blob worker pathnames often look like a UUID-style path under a blob: URL—not like an http(s) docs path. Always log pathname together with href when debugging.
No. WorkerLocation.pathname is read-only.
Same idea (URL path), different object. Inside a worker you read WorkerLocation via self.location—not the page’s Location.

Next: port

Read the port part of the worker script URL.

port property →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful