JavaScript WorkerLocation search Property

Beginner
5 min read
Updated: Nov 2026
5 examples
Property

Definition

WorkerLocation.search returns the search (query) part of the worker’s location as a string. Per MDN, if the worker URL has ?t=67, location.search is "?t=67".

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

Syntax

Use location.search. Start with this line.

JavaScript
self.location.search
CallWhat it does
self.location.searchPrimary signature.

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

Library declarations
JavaScript
self.location.search

Parameters

ParameterDescription
Worker contextRequired. self.location is a WorkerLocation

Return Value

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

JavaScript Examples

Example 1 — Read location.search

When the worker URL has ?t=67, the property returns that query (MDN idea).

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

How It Works

  1. Pass a query. Main starts a same-origin worker with ?t=67 on the script URL.
  2. Read inside the worker. location.search is the query part of that script URL.
  3. Leading ?. The returned string includes ? when a query exists.
  4. Send it out. The demo worker posts the value so the page can display it.

Example 2 — search vs hash

Query and fragment are separate — both can appear on the same Worker URL.

JavaScript
// Worker URL: …?t=67#demo
self.postMessage({
  search: location.search, // "?t=67"
  hash: location.hash      // "#demo"
});
Try It Yourself

How It Works

  1. Append both parts. Use ?… then #… on the Worker URL.
  2. Read search. Only the query string — not the fragment.
  3. Read hash. Only the fragment — not the query.
  4. Keep them separate. Do not treat search as a stand-in for hash.

Example 3 — Empty vs Query

Without a query on the Worker URL, search is an empty string.

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

How It Works

  1. No ?… on the URL. The worker script URL has no query.
  2. Empty string. location.search is "", not null.
  3. Add a query. new Worker(url + "?mode=demo") makes search non-empty.
  4. Check clearly. Use search === "" before parsing parameters.

Example 4 — Parse with URLSearchParams

Turn ?t=67 into a named value with get("t").

JavaScript
// Main receives search from the worker, then:
const search = "?t=67";
const params = new URLSearchParams(search);
console.log(params.get("t")); // "67"
Try It Yourself

How It Works

  1. Get the raw string. Worker posts location.search (includes ?).
  2. Wrap in URLSearchParams. It accepts a query string with or without ?.
  3. Read a key. get("t") returns "67" (or null if missing).
  4. Stay on the page if needed. Parsing on the main thread after postMessage is fine for demos.

Example 5 — Worker vs Page search

The worker query is not the same object as the page query.

JavaScript
// Inside worker (URL has ?t=67):
self.postMessage(location.search);

// On the page:
// worker search → "?t=67"
// page search   → window.location.search (often "")
Try It Yourself

How It Works

  1. Worker has its own URL. Query comes from the Worker script URL.
  2. Page has window.location. That is a different Location object.
  3. Values can differ. Appending ?t=67 to the worker does not change the page URL.
  4. Pick the right one. Inside a worker, always use self.location.search.

Browser Support

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

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

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

Frequently Asked Questions

A string: the search (query) part of the worker’s location URL—including the leading ? when a query is present. 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 the empty string "" when the worker URL has no query parameters (not null).
Yes. Pass the search string into URLSearchParams—for example new URLSearchParams(location.search)—then use get("key").
No. WorkerLocation.search is read-only. Put the query on the Worker URL when you create the worker.

Next: toString()

Serialize the full worker location as a string.

toString() →

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