JavaScript WorkerLocation

Beginner
8 min read
Updated: Nov 2026
3 examples
Worker URL

Definition and Usage

WorkerLocation is the type of self.location inside a Web Worker. It describes the URL of the worker script, similar in spirit to window.location but read-focused for workers.

Read href, pathname, search, and hash. toString() returns the full URL string. You cannot assign location.href to navigate the way a window can.

This page teaches what WorkerLocation is. Method and property guides live under this section.

Only available inside workers.

Page scripts use window.location (a Location object). Worker scripts use self.location (WorkerLocation).

JavaScript Syntax

Reading location inside worker.js:

JavaScript
console.log(self.location.href);
console.log(self.location.pathname);
console.log(self.location.toString());

Core Ideas

Ideas every WorkerLocation tutorial builds on:

IdeaExampleRemember
Whereself.locationInside the worker only
hreflocation.hrefFull script URL
pathnamelocation.pathnamePath of the script
searchlocation.searchQuery string including ?
toStringlocation.toString()Same as href string
No navigatelocation.href = …Not for page navigation

JavaScript Examples

Three short programs that show how WorkerLocation works.

Example 1 — Read the worker href

href is the absolute URL of the worker script.

JavaScript
console.log(typeof self.location.href);
console.log(self.location.href.includes("http"));

How It Works

  1. href is a string URL.
  2. It points at the worker file that is running.
  3. Useful for resolving relative imports in workers.

Example 2 — Read pathname

pathname is the path portion of that URL.

JavaScript
console.log(self.location.pathname.endsWith(".js"));

How It Works

  1. pathname excludes protocol and host.
  2. Worker scripts often end with .js.
  3. Compare paths when you load helpers relative to the worker.

Example 3 — toString() equals href

Calling toString returns the full URL string.

JavaScript
console.log(self.location.toString() === self.location.href);

How It Works

  1. toString() stringifies the location.
  2. It matches href for WorkerLocation.
  3. Handy when an API expects a string URL.

Next: WorkerLocation Guides

Continue with toString and the location properties.

Look upWhy
toString()Full URL as a string
hrefAbsolute worker script URL
pathnamePath of the script
searchQuery string

Full lists: toString().

Frequently Asked Questions

WorkerLocation is the object you get from self.location inside a Web Worker. It describes the worker script’s URL with href, pathname, and related fields.
Both describe URLs, but WorkerLocation is for worker scripts and does not support the same navigation assignments a window Location does.
Yes. Use self.location.search and parse it, or pass data with postMessage from the page instead.
No. Pages use Location via window.location. WorkerLocation appears only in worker scopes.
Open toString(), then the Worker hub for creating workers and messaging.

Open toString()

Then browse href, pathname, and search properties.

toString() →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

12 people found this page helpful