JavaScript WorkerLocation href Property

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

What You’ll Learn

The href property of a WorkerLocation object returns a string containing the serialized URL for the worker’s location. Learn self.location.href, how it differs from pieces like host, and how blob workers expose blob: URLs—with five examples and try-it labs.

01

Kind

Instance property

02

Returns

String (full URL)

03

Access

self.location

04

Writable?

Read-only

05

Context

Web Workers only

06

Status

Baseline widely

Introduction

Inside a Web Worker, self.location is a WorkerLocation for the worker script URL. href is the complete serialized form of that URL—the whole address as one string.

MDN’s classic idea: for a worker loaded from an https docs URL, location.href returns that full URL string. In our try-it labs, blob workers return a blob:… URL instead.

💡
Beginner tip

When host / hostname are empty on a blob worker, href is still the best property for logging “which script URL is this worker running?”

Understanding the href Property

A read-only instance property on WorkerLocation that returns the serialized URL of the worker’s location as a string.

  • Value — a string (the full worker script URL).
  • SynonymWorkerLocation.toString() returns the same serialized URL.
  • Accessself.location.href or location.href 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 = location.href;
// full serialized worker URL string

Value

A string.

Typical pattern (MDN idea)

JavaScript
// Inside a Web Worker
const result = location.href;
// e.g. "https://developer.mozilla.org/en-US/docs/Web"
self.postMessage(result);

⚡ Quick Reference

GoalCode / note
Read hrefself.location.href
Return typeString (full URL)
Parse partsnew URL(location.href)
Blob workersStarts with blob:
Send to mainself.postMessage(location.href)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about WorkerLocation.href.

Returns
full URL

Serialized string

Where
in worker

self.location

Writable
no

Read-only

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN WorkerLocation.href. Labs use blob workers so you always get a concrete full URL string.

📚 Getting Started

Read the full URL the MDN way.

Example 1 — Read location.href (MDN Idea)

Post the serialized worker URL to the main thread.

JavaScript
const result = location.href;
self.postMessage(result);
Try It Yourself

How It Works

On an https worker script, you would see a normal https URL; blob demos show a blob: URL instead.

Example 2 — Blob Worker href

Confirm the worker URL starts with blob: and still has a non-empty href.

JavaScript
self.postMessage({
  href: location.href,
  isBlob: location.href.indexOf("blob:") === 0,
  hostnameEmpty: location.hostname === ""
});
Try It Yourself

How It Works

This shows why demos prefer href over hostname for blob workers.

📈 Parse & Compare

Break the URL apart and contrast with the page.

Example 3 — Parse with the URL Constructor

Turn href into structured parts when you need them.

JavaScript
const u = new URL(location.href);
self.postMessage({
  href: location.href,
  protocol: u.protocol,
  pathname: u.pathname
});
Try It Yourself

How It Works

You can also read location.protocol / pathname directly; new URL(href) is handy for one-shot parsing.

Example 4 — Worker href vs Page href

The worker script URL is not the same as the HTML page URL.

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

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

How It Works

Never assume the worker’s href matches the tab’s address bar.

Example 5 — Protocol Check from href

Branch on whether the worker is a blob URL or an http(s) script.

JavaScript
const href = location.href;
self.postMessage({
  href,
  kind: href.indexOf("blob:") === 0 ? "blob" : "other"
});
Try It Yourself

How It Works

Prefer location.protocol === "blob:" in production code; checking href is a beginner-friendly alternative.

🚀 Common Use Cases

  • Log the exact worker script URL for debugging.
  • Detect blob vs http(s) workers from one string.
  • Parse the worker URL with new URL(location.href).
  • Send the worker URL to the main thread for diagnostics panels.
  • Confirm a worker was started from the URL you intended.

🔧 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 href

location.href returns the full serialized URL string.

Read
4

Use or post

Log it, parse it, or postMessage the string 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 full URL string; toString() is a synonym.
  • Not the same as window.location.href on the page.
  • Related learning: hostname, host, hash, Worker().

Universal Browser Support

WorkerLocation.href 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.href

Returns the serialized URL for the worker's location as a string. Read it with self.location.href 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 Workers / WorkerLocation in IE10+ (prefer modern browsers)
Legacy
WorkerLocation.href Excellent

Bottom line: Read location.href inside the worker for the full script URL—especially useful when host/hostname are empty on blob: workers.

Conclusion

WorkerLocation.href is the full serialized worker script URL. Read it inside the worker, post it when the UI needs diagnostics, and prefer it over empty host values on blob workers.

Continue with origin, hostname, host, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read href only inside a worker
  • Use href for logging full worker script URLs
  • Parse with new URL(location.href) when needed
  • Use postMessage to surface the value on the page
  • Prefer protocol for scheme checks in production

❌ Don’t

  • Expect WorkerLocation on the main thread
  • Assign to location.href in a worker (read-only)
  • Assume worker href equals the page address bar
  • Ignore blob URLs when debugging empty hostname
  • Put secrets in worker URLs—they can be logged

Key Takeaways

Knowledge Unlocked

Five things to remember about WorkerLocation.href

The full serialized worker script URL, as a string.

5
Core concepts
⚙️02

Workers

self.location

Context
🔒03

Read-only

no assign

Rule
📄04

vs parts

whole URL

Compare
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

A string containing the serialized URL for the worker's location—the full worker script URL. Access it inside a worker as self.location.href or location.href.
No. MDN marks WorkerLocation.href 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 complete serialized URL. host / hostname / pathname / search / hash are individual pieces of that URL.
A blob: URL string such as blob:http://localhost:3002/<uuid>. Unlike host/hostname (often empty on blobs), href is still a useful full URL.
No. WorkerLocation.href is read-only. Assigning to it does not navigate the worker the way window.location.href can on a page.
On WorkerLocation, toString() is documented as a synonym for href—both give the serialized worker URL string.
Did you know?

MDN documents WorkerLocation.toString() as a synonym for href. Both give you the same serialized worker URL string—handy when an API expects a stringifiable location object.

Next: WorkerLocation.origin

Learn how to read the worker's origin string.

origin 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