JavaScript WorkerLocation search Property

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

What You’ll Learn

The search property of a WorkerLocation object returns the search (query string) of the worker’s location. Learn self.location.search, the leading ?, how it differs from hash and href, and how to demo it with blob worker URLs—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 search (query string) is the part of a URL that starts with ?—for example ?t=67 in https://example.com/path?t=67. Inside a worker, self.location.search returns that query for the worker script URL.

MDN’s classic idea: on a worker related to a URL with ?t=67, location.search can look like "?t=67".

💡
Beginner tip

No query means search is often "" (empty). The fragment after # is hash, not search.

Understanding the search Property

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

  • Value — a string (e.g. "?t=67" or "").
  • vs hash — search is ?…; hash is #….
  • Accessself.location.search or location.search 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.search;
// e.g. "?t=67"

Value

A string.

Typical pattern (MDN idea)

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

Demo tip: append a query to a blob worker URL

JavaScript
const base = URL.createObjectURL(new Blob(
  ["self.postMessage(location.search);"],
  { type: "application/javascript" }
));
const worker = new Worker(base + "?t=67");

⚡ Quick Reference

GoalCode / note
Read searchself.location.search
Return typeString
MDN example shape"?t=67"
No queryOften ""
Send to mainself.postMessage(location.search)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about WorkerLocation.search.

Returns
search

String

Where
in worker

self.location

Writable
no

Read-only

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN WorkerLocation.search. Labs append ?… to blob worker URLs so you can see a real query string.

📚 Getting Started

Read the search string the MDN way.

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

Create a worker with ?t=67 and post the search string.

JavaScript
const result = self.location.search;
// e.g. "?t=67"
self.postMessage(result);
Try It Yourself

How It Works

The try-it lab builds a blob worker URL and appends ?t=67, matching MDN’s sample shape.

Example 2 — search vs hash

Query and fragment are different URL parts.

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

How It Works

The lab uses ?t=67#demo so both properties return non-empty values.

📈 Empty Queries & Parsing

See empty search, then parse a query with URLSearchParams.

Example 3 — Empty Search vs With Query

Compare a plain blob worker with one that has a query string.

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

// Main: one Worker(base), one Worker(base + "?mode=demo")
Try It Yourself

How It Works

Same worker code, different script URLs—only the URL with ? reports a non-empty search.

Example 4 — Parse With URLSearchParams

Post the search string; parse a named parameter on the main thread.

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

// Main
worker.onmessage = (e) => {
  const params = new URLSearchParams(e.data);
  console.log(params.get("t")); // "67"
};
Try It Yourself

How It Works

URLSearchParams accepts the leading ?, so you can read keys like t easily.

Example 5 — Worker Search vs Page Search

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

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

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

How It Works

The page search is whatever is in the browser address bar; the worker search comes from the worker script URL.

🚀 Common Use Cases

  • Pass config flags to a worker via the script URL query.
  • Log the worker query string for debugging.
  • Parse keys with URLSearchParams.
  • Distinguish query (search) from fragment (hash).
  • Teach URL query concepts using workers.

🔧 How It Works

1

Worker URL

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

URL
2

WorkerLocation

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

API
3

Read search

location.search returns the query string (or "" if none).

Read
4

Use or post

Parse, log, or postMessage the search 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 string; usually starts with ? when present.
  • You can append ?… to blob worker URLs for demos (same idea as hash labs).
  • Related learning: protocol, hash, href, Worker().

Universal Browser Support

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

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

Bottom line: Read location.search inside the worker—empty means no query. Pair with hash and URLSearchParams when needed.

Conclusion

WorkerLocation.search is the worker-side query string for the script URL. Read it inside the worker, expect an empty string when there is no query, and use URLSearchParams when you need named parameters.

Continue with toString(), href, hash, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read search only inside a worker
  • Append ?key=value to demo worker URLs
  • Parse with URLSearchParams when you need keys
  • Keep search and hash separate in your mental model
  • Use postMessage to surface the value on the page

❌ Don’t

  • Expect WorkerLocation on the main thread
  • Assign to location.search in a worker (read-only)
  • Confuse ? query with # hash
  • Assume every worker URL has a non-empty search
  • Forget the leading ? when comparing sample strings

Key Takeaways

Knowledge Unlocked

Five things to remember about WorkerLocation.search

The worker script’s query string.

5
Core concepts
⚙️02

Workers

self.location

Context
🔒03

Read-only

no assign

Rule
📄04

vs hash

? vs #

Compare
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

A string: the search (query) part of the worker's location. When present it usually starts with "?". Access it inside a worker as self.location.search or location.search.
No. MDN marks WorkerLocation.search as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is only available in Web Workers.
search is the query string (starts with ? when present). hash is the fragment (starts with # when present). They are separate parts of the URL.
location.search is often the empty string "" when the worker URL has no query parameters.
Yes. On the main thread (or in modern workers), you can pass the search string into URLSearchParams—for example new URLSearchParams(location.search).
No. WorkerLocation.search is read-only.
Did you know?

MDN’s example returns ?t=67 including the question mark. You can append the same kind of query to a blob worker URL (new Worker(blobUrl + "?t=67")—just like these try-it labs.

Next: WorkerLocation.toString()

Learn how to serialize the worker URL as a string.

toString() method →

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