JavaScript WorkerLocation origin Property

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

What You’ll Learn

The origin property of a WorkerLocation object returns the worker’s origin as a string. Learn self.location.origin, how it differs from href and host, 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

An origin is the combination of scheme, host, and port that defines a security boundary on the web. Inside a worker, self.location.origin returns that origin for the worker script URL.

MDN’s classic idea: on a page like the MDN docs site, self.location.origin can look like "https://developer.mozilla.org:443".

💡
Beginner tip

origin has no path, query, or hash. For the full worker URL string, use href.

Understanding the origin Property

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

  • Value — a string (origin form).
  • vs href — origin omits path, search, and hash.
  • Accessself.location.origin or location.origin inside the worker.
  • Workers only — not available on the main document thread.
  • Baseline Widely available on MDN (since September 2016).

📝 Syntax

Read the property inside a worker script:

JavaScript
const result = self.location.origin;
// e.g. "https://developer.mozilla.org:443"

Value

A string.

Typical pattern (MDN idea)

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

⚡ Quick Reference

GoalCode / note
Read originself.location.origin
Return typeString
MDN example shape"https://developer.mozilla.org:443"
Send to mainself.postMessage(location.origin)
MDN statusBaseline Widely available (since September 2016)

🔍 At a Glance

Four facts to remember about WorkerLocation.origin.

Returns
origin

String

Where
in worker

self.location

Writable
no

Read-only

Baseline
widely

Since Sep 2016

Examples Gallery

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

📚 Getting Started

Read the origin the MDN way.

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

Post the worker origin string to the main thread.

JavaScript
const result = self.location.origin;
// e.g. "https://developer.mozilla.org:443"
self.postMessage(result);
Try It Yourself

How It Works

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

Example 2 — origin vs href

Origin is shorter; href includes the full worker URL.

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

How It Works

Use origin for security / same-origin checks; use href when you need the exact script URL.

📈 Blob & Page Comparison

See how blob workers and the page origin relate.

Example 3 — Blob Worker Origin

Log origin together with protocol and href for blob workers.

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

How It Works

Blob origins can be opaque ("null") or otherwise differ from a normal https origin—always inspect the live value.

Example 4 — Worker Origin vs Page Origin

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

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

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

How It Works

Same-origin http(s) workers usually match the page; blob workers often do not look identical.

Example 5 — Same Origin as the Page?

Main compares the posted worker origin with its own origin.

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

// Main
worker.onmessage = (e) => {
  console.log(e.data === location.origin);
};
Try It Yourself

How It Works

A dedicated http(s) worker from your site would usually report true; blob demos often report false.

🚀 Common Use Cases

  • Log the worker origin for security / debugging panels.
  • Compare worker origin with the page origin.
  • Distinguish blob workers from http(s) workers.
  • Pair with href when diagnosing script URL issues.
  • Teach same-origin 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 origin

location.origin returns the origin string for that URL.

Read
4

Use or post

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

📝 Notes

  • MDN: Baseline Widely available (since September 2016) — no Deprecated / Experimental / Non-standard banner.
  • Only available in Web Workers (WorkerLocation / self.location).
  • Read-only string; no path, search, or hash.
  • Blob worker origins can differ from normal http(s) origins.
  • Related learning: href, host, hostname, Worker().

Universal Browser Support

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

Baseline · Widely available

WorkerLocation.origin

Returns the worker's origin as a string. Read it with self.location.origin 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.origin Excellent

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

Conclusion

WorkerLocation.origin is the worker-side origin 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 pathname, href, host, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read origin only inside a worker
  • Compare with window.location.origin on the main thread
  • Log href alongside blob origins
  • Use postMessage to surface the value on the page
  • Treat opaque / unexpected origins carefully

❌ Don’t

  • Expect WorkerLocation on the main thread
  • Assign to location.origin in a worker (read-only)
  • Confuse origin with the full href
  • Assume blob demos always match MDN’s https example
  • Use origin alone when you need path or query details

Key Takeaways

Knowledge Unlocked

Five things to remember about WorkerLocation.origin

The worker script’s origin, as a string.

5
Core concepts
⚙️02

Workers

self.location

Context
🔒03

Read-only

no assign

Rule
📄04

vs href

no path/hash

Compare
🎯05

Baseline

since Sep 2016

Status

❓ Frequently Asked Questions

A string: the worker's origin (scheme + host + port in origin form). Access it inside a worker as self.location.origin or location.origin.
No. MDN marks WorkerLocation.origin as Baseline Widely available (since September 2016). It is not Deprecated, Experimental, or Non-standard. It is only available in Web Workers.
href is the full serialized worker URL (path, query, hash included). origin is only the origin part—no path or fragment.
Blob worker origins can look different from http(s) workers (sometimes the string "null" for an opaque origin, depending on the URL and browser). Always log origin together with href when debugging.
No. WorkerLocation.origin is read-only.
Same idea (URL origin), different object. Inside a worker you read WorkerLocation via self.location—not the page's Location.
Did you know?

MDN’s example shows a default HTTPS port in the origin string (:443. Some browsers serialize default ports differently for display—always trust the live location.origin value in your environment.

Next: WorkerLocation.pathname

Learn how to read the worker's pathname string.

pathname 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