JavaScript WorkerLocation host Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

WorkerLocation.host returns the host part of the worker’s location as a string—usually hostname plus port when shown. Per MDN, on http://localhost:8080/, location.host is "localhost:8080".

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

Syntax

Use location.host. Start with this line.

JavaScript
location.host
CallWhat it does
location.hostPrimary signature.

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

Library declarations
JavaScript
location.host

Parameters

ParameterDescription
Worker contextRequired. self.location is a WorkerLocation

Return Value

Return ValueDescription
*String ("host:port" or "")

JavaScript Examples

Example 1 — Read location.host

Post the host string (MDN idea: localhost:8080 on http workers).

JavaScript
// Inside a Web Worker on http://localhost:8080/
const result = location.host; // "localhost:8080"
self.postMessage(result);
Try It Yourself

How It Works

  1. Worker context. Inside the worker, self.location is a WorkerLocation.
  2. Read host. location.host returns the host part of that script URL.
  3. MDN shape. On http://localhost:8080/, the value is "localhost:8080".
  4. Blob labs differ. Try-it demos use blob workers, so the live host is often "" while href still shows the blob URL.

Example 2 — Empty Host on Blob Workers

Blob workers often report an empty host—check protocol too.

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

How It Works

  1. Blob URL. URL.createObjectURL workers use a blob: script URL.
  2. Empty host. Those URLs often have no normal hostname → host === "".
  3. Confirm with protocol. location.protocol is typically "blob:".
  4. Not a bug. Prefer href when you need the full blob URL string.

Example 3 — host vs hostname

Compare host, hostname, and port on the same worker location.

JavaScript
self.postMessage({
  host: location.host,
  hostname: location.hostname,
  port: location.port
});
Try It Yourself

How It Works

  1. Three pieces. Read host, hostname, and port from the same location.
  2. host combines. When a port is shown, host looks like hostname:port.
  3. hostname is bare. No port number in hostname.
  4. Pick the right one. Need authority? Use host. Need name or port alone? Use those properties.

Browser Support

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

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

Bottom line: Browser / Web Workers only — not a Node.js API. Expect MDN-style hosts on http(s) scripts; blob demos often print an empty host.

Frequently Asked Questions

A string: the host part of the worker’s location URL—typically hostname plus port when the port is non-default (for example "localhost:8080"). Access it inside a worker as self.location.host or location.host.
No. MDN marks WorkerLocation.host as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is only available in Web Workers.
hostname is just the name (for example "localhost"). host is hostname plus port when applicable (for example "localhost:8080"). Prefer host when you need the full authority string.
blob: worker URLs often have an empty host. That is normal. MDN’s localhost:8080 example applies to http(s) worker script URLs. Always check location.protocol and location.href too.
No. WorkerLocation.host is read-only. You cannot rewrite the worker URL by assigning to host.
Same idea (URL host), different object. Inside a worker you read WorkerLocation via self.location—not the page’s Location.

Next: hostname

Read the hostname without the port number.

hostname 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