JavaScript WorkerLocation hash Property

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

What You’ll Learn

The hash property of a WorkerLocation object returns the hash (fragment) part of the worker’s location. Learn self.location.hash, the string return value, how it differs from search and the page’s window.location.hash—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

Inside a Web Worker, self.location is a WorkerLocation—the absolute URL of the script that worker is running. The hash property is the fragment after #, just like the familiar page API, but scoped to the worker script URL, not the HTML page.

MDN’s classic example: on a worker loaded from a URL ending in #examples, location.hash returns "#examples".

💡
Beginner tip

You cannot read WorkerLocation from the main page. Start a worker, read location.hash inside it, then postMessage the value back if the UI needs it.

Understanding the hash Property

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

  • Value — a string (includes leading # when a fragment exists).
  • Empty fragment"" when the worker URL has no hash.
  • Accessself.location.hash or location.hash 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.hash;
// e.g. "#examples" when the worker URL ends with #examples

Value

A string.

Typical pattern (MDN idea)

JavaScript
// Inside a Web Worker whose URL includes #examples
const result = location.hash; // "#examples"
self.postMessage(result);

⚡ Quick Reference

GoalCode / note
Read hashself.location.hash
Return typeString
No fragment""
Pass fragment into workernew Worker(url + "#examples")
Send to mainself.postMessage(location.hash)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about WorkerLocation.hash.

Returns
string

Often "#id"

Where
in worker

self.location

Writable
no

Read-only

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN WorkerLocation.hash. Labs use blob workers and append #… to the worker URL so location.hash is easy to see.

📚 Getting Started

Read the fragment the MDN way.

Example 1 — Read location.hash (MDN Idea)

When the worker URL ends with #examples, the property returns that fragment.

JavaScript
// Worker URL: …blob:…#examples
const result = location.hash; // "#examples"
self.postMessage(result);
Try It Yourself

How It Works

The fragment is part of the URL you pass to new Worker(...). The worker sees it on WorkerLocation.

Example 2 — Empty Hash

Without a fragment, hash is an empty string.

JavaScript
self.postMessage({
  hash: location.hash,
  isEmpty: location.hash === ""
});
Try It Yourself

How It Works

Check hash === "" (or !hash) before treating it as a section id.

📈 Parts & Messaging

Compare URL pieces and send the hash to the main thread.

Example 3 — hash vs search

Fragments and query strings are different parts of the URL.

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

How It Works

search is the ?query part; hash is the #fragment part.

Example 4 — Post Hash to the Main Thread

Main creates the worker with a fragment; the worker replies with location.hash.

JavaScript
// Main
const worker = new Worker(blobUrl + "#section-a");
worker.onmessage = (e) => console.log(e.data);

// Worker
self.postMessage(location.hash);
Try It Yourself

How It Works

Useful when you encode a mode or section id in the worker URL fragment for configuration.

Example 5 — Strip the Leading #

Convert #examples into a plain id string for comparisons.

JavaScript
const raw = location.hash;           // "#examples"
const id = raw.startsWith("#") ? raw.slice(1) : raw;
self.postMessage({ raw, id });
Try It Yourself

How It Works

Browsers include the # character in the returned string when a fragment exists.

🚀 Common Use Cases

  • Pass a mode or section id into a worker via the URL fragment.
  • Log which fragment a shared worker script URL was started with.
  • Branch worker startup based on location.hash.
  • Debug blob-worker demos by tagging runs with #demo-1, #demo-2.
  • Keep query params (search) and fragments (hash) separate in protocols.

🔧 How It Works

1

Worker URL

Main thread creates a Worker with a script URL (optionally including #fragment).

URL
2

WorkerLocation

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

API
3

Read hash

location.hash returns the fragment string (or "").

Read
4

Use or post

Branch logic in the worker, or postMessage the value 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; empty string when there is no fragment.
  • Not the same object as window.location on the page.
  • Related learning: Worker(), postMessage(), message.

Universal Browser Support

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

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

Bottom line: Read location.hash inside the worker—append #fragment to the Worker URL when you need a non-empty value in demos.

Conclusion

WorkerLocation.hash is the worker-side fragment string for the script URL. Read it with location.hash inside the worker, remember it is read-only, and post it to the main thread when the UI needs the value.

Continue with host, Worker(), postMessage(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read hash only inside a worker
  • Treat missing fragments as ""
  • Strip the leading # when you need a bare id
  • Use postMessage to surface the value on the page
  • Keep hash and search roles clear in your URL design

❌ Don’t

  • Expect WorkerLocation on the main thread
  • Assign to location.hash in a worker (read-only)
  • Confuse page window.location.hash with the worker’s hash
  • Assume blob workers always have a fragment
  • Put secrets in the fragment—URLs can be logged

Key Takeaways

Knowledge Unlocked

Five things to remember about WorkerLocation.hash

The fragment of the worker script URL, as a string.

5
Core concepts
⚙️02

Workers

self.location

Context
🔒03

Read-only

no assign

Rule
💬04

postMessage

send to main

Pattern
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

A string: the hash (fragment) part of the worker's location URL—including the leading # when a fragment is present. Access it inside a worker as self.location.hash or location.hash.
No. MDN marks WorkerLocation.hash as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is only available in Web Workers.
Same idea (URL fragment), different object. Inside a worker you read WorkerLocation via self.location—not the page's Location. Also, WorkerLocation.hash is read-only.
If the worker script URL has no fragment, location.hash is the empty string "" (not null).
No. WorkerLocation.hash is read-only. You cannot navigate or rewrite the worker URL by assigning to hash the way you might on window.location.
Pass a fragment on the Worker URL, for example new Worker(blobUrl + "#examples"). Inside the worker, location.hash is then "#examples".
Did you know?

WorkerLocation is initialized for each worker and exposed as self.location. It mirrors many ideas from page Location, but it is a separate interface that exists only inside worker scripts.

Next: WorkerLocation.host

Learn how to read the host part of a worker script URL.

host 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