JavaScript WorkerLocation pathname Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

The pathname property of a WorkerLocation object returns the pathname of the worker’s location as a string. Learn self.location.pathname, how it differs from href, search, and hash, and what blob workers report—with five examples and try-it labs.

01

Kind

Instance property

02

Returns

String

03

Access

self.location

04

Writable?

Read-only

05

Context

Web Workers only

06

Status

Baseline widely

Introduction

A pathname is the path portion of a URL—everything after the host (and port), before the query string or hash. Inside a worker, self.location.pathname returns that path for the worker script URL.

MDN’s classic idea: on a worker loaded like the MDN docs path, location.pathname can look like "/en-US/docs/Web".

💡
Beginner tip

pathname has no query (search) or fragment (hash). For the full worker URL string, use href.

Understanding the pathname Property

A read-only instance property on WorkerLocation that returns the worker’s pathname as a string.

  • Value — a string (often starting with /).
  • vs href — pathname omits scheme, host, search, and hash.
  • Accessself.location.pathname or location.pathname inside the worker.
  • Workers only — not available on the main document thread.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Read the property inside a worker script:

JavaScript
const result = self.location.pathname;
// e.g. "/en-US/docs/Web"

Value

A string.

Typical pattern (MDN idea)

JavaScript
// Inside a Web Worker
const result = self.location.pathname;
self.postMessage(result);

⚡ Quick Reference

GoalCode / note
Read pathnameself.location.pathname
Return typeString
MDN example shape"/en-US/docs/Web"
Send to mainself.postMessage(location.pathname)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about WorkerLocation.pathname.

Returns
pathname

String

Where
in worker

self.location

Writable
no

Read-only

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN WorkerLocation.pathname. Labs use blob workers and compare pathname with related URL parts.

📚 Getting Started

Read the pathname the MDN way.

Example 1 — Read self.location.pathname (MDN Idea)

Post the worker pathname string to the main thread.

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

How It Works

The try-it lab posts the live blob worker’s pathname plus the MDN sample string for comparison.

Example 2 — pathname vs href

Pathname is only the path; href is the full worker URL.

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

How It Works

Use pathname when you only need the path; use href when you need the exact script URL.

📈 Blob & URL Parts

See how blob workers and related URL pieces relate.

Example 3 — Blob Worker Pathname

Log pathname together with protocol and href for blob workers.

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

How It Works

Blob pathnames often look like a UUID path—not like MDN’s /en-US/docs/Web sample. Always inspect the live value.

Example 4 — Pathname vs Search vs Hash

Post the three URL pieces so you can see what each property covers.

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

How It Works

Simple blob workers often have empty search and hash; the interesting part is still pathname.

Example 5 — Worker Pathname vs Page Pathname

Post the worker pathname; compare it with window.location.pathname.

JavaScript
// Worker
self.postMessage(location.pathname);

// Main
worker.onmessage = (e) => {
  console.log("worker:", e.data);
  console.log("page:", location.pathname);
};
Try It Yourself

How It Works

The page path is the tutorial URL; the worker path comes from its own script URL (often a blob UUID path in these labs).

🚀 Common Use Cases

  • Log the worker path for debugging panels.
  • Compare worker pathname with the page pathname.
  • Distinguish blob workers from http(s) workers by path shape.
  • Pair with href, search, and hash when diagnosing URLs.
  • Teach URL path concepts using workers.

🔧 How It Works

1

Worker URL

Main creates a Worker with an http(s), blob, or other script URL.

URL
2

WorkerLocation

Inside the worker, self.location describes that absolute script URL.

API
3

Read pathname

location.pathname returns the path string for that URL.

Read
4

Use or post

Compare, log, or postMessage the pathname to the main thread.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Only available in Web Workers (WorkerLocation / self.location).
  • Read-only string; no query or hash.
  • Blob worker pathnames can differ a lot from normal http(s) paths.
  • Related learning: origin, href, hash, Worker().

Universal Browser Support

WorkerLocation.pathname is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. It is only available in Web Workers.

Baseline · Widely available

WorkerLocation.pathname

Returns the worker's pathname as a string. Read it with self.location.pathname inside a worker.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Limited / legacy workers path (prefer modern browsers)
Legacy
WorkerLocation.pathname Excellent

Bottom line: Read location.pathname inside the worker—compare with the page path, and pair with href when debugging blob workers.

Conclusion

WorkerLocation.pathname is the worker-side path string for the script URL. Read it inside the worker, compare it with the page when needed, and use href when you need the full URL.

Continue with port, origin, href, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read pathname only inside a worker
  • Compare with window.location.pathname on the main thread
  • Log href alongside blob pathnames
  • Use postMessage to surface the value on the page
  • Pair with search and hash when parsing URLs

❌ Don’t

  • Expect WorkerLocation on the main thread
  • Assign to location.pathname in a worker (read-only)
  • Confuse pathname with the full href
  • Assume blob demos always match MDN’s http(s) path example
  • Expect query or hash inside pathname

Key Takeaways

Knowledge Unlocked

Five things to remember about WorkerLocation.pathname

The worker script’s path, as a string.

5
Core concepts
⚙️02

Workers

self.location

Context
🔒03

Read-only

no assign

Rule
📄04

vs href

path only

Compare
🎯05

Baseline

since Jul 2015

Status

❓ 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.
Did you know?

MDN’s example uses a docs-style path (/en-US/docs/Web. Blob workers in these try-it labs usually show a UUID-style pathname instead—same property, different URL shape.

Next: WorkerLocation.port

Learn how to read the worker's port string.

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.

5 people found this page helpful