JavaScript WorkerLocation hash Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

WorkerLocation.hash returns the hash (fragment) part of the worker’s location as a string. Per MDN, if the worker URL ends with #examples, location.hash is "#examples".

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

Syntax

Use location.hash. Start with this line.

JavaScript
location.hash
CallWhat it does
location.hashPrimary signature.

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

Library declarations
JavaScript
location.hash

Parameters

ParameterDescription
Worker contextRequired. self.location is a WorkerLocation
Fragment on Worker URLOptional. Needed for a non-empty hash

Return Value

Return ValueDescription
*String ("#…" or "")

JavaScript Examples

Example 1 — Read location.hash

When the worker URL ends with #examples, the property returns that fragment (MDN idea).

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

How It Works

  1. Pass a fragment. Main starts the worker with new Worker(url + "#examples").
  2. Read inside the worker. location.hash is the fragment of that script URL.
  3. Leading #. The returned string includes # when a fragment exists.
  4. Send it out. postMessage(result) lets the page display the value.

Example 2 — Empty Hash

Without a fragment on the Worker URL, hash is an empty string.

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

How It Works

  1. No #… on the URL. The worker script URL has no fragment.
  2. Empty string. location.hash is "", not null or undefined.
  3. Check clearly. Use hash === "" (or !hash) before treating it as an id.
  4. Still a string. Always safe to call string methods; just handle the empty case.

Example 3 — Strip the Leading #

Convert #examples into a plain id for comparisons or branching.

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

How It Works

  1. Read the raw fragment. Browsers include the leading # when present.
  2. Detect the prefix. startsWith("#") is a clear check (works for "" too).
  3. Slice once. slice(1) drops the first character → "examples".
  4. Use the bare id. Compare or switch on id without worrying about #.

Browser Support

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

Supported across modern browsers with Web Workers. Read location.hash 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.hash Excellent

Bottom line: Browser / Web Workers only — not a Node.js API. Append #fragment to the Worker URL when demos need a non-empty hash.

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".

Next: host

Read the host part of the 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.

8 people found this page helpful