JavaScript WorkerLocation hostname Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

WorkerLocation.hostname returns the hostname part of the worker’s location as a string—the name only, without the port. Per MDN, on http://localhost:8080/, location.hostname is "localhost".

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

Syntax

Use location.hostname. Start with this line.

JavaScript
location.hostname
CallWhat it does
location.hostnamePrimary signature.

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

Library declarations
JavaScript
location.hostname

Parameters

ParameterDescription
Worker contextRequired. self.location is a WorkerLocation

Return Value

Return ValueDescription
*String (name only, or "")

JavaScript Examples

Example 1 — Read location.hostname

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

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

How It Works

  1. Worker context. Inside the worker, self.location is a WorkerLocation.
  2. Read hostname. location.hostname returns the name part of that script URL.
  3. No port. On http://localhost:8080/, the value is "localhost", not "localhost:8080".
  4. Blob labs differ. Try-it demos use blob workers, so the live hostname is often "".

Example 2 — Empty Hostname on Blob Workers

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

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

How It Works

  1. Blob URL. URL.createObjectURL workers use a blob: script URL.
  2. Empty hostname. Those URLs often have no normal hostname → hostname === "".
  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 — hostname vs host

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

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

How It Works

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

Browser Support

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

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

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

Frequently Asked Questions

A string: the hostname part of the worker’s location URL—without the port (for example "localhost" on http://localhost:8080/). Access it inside a worker as self.location.hostname or location.hostname.
No. MDN marks WorkerLocation.hostname 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").
blob: worker URLs often have an empty hostname. That is normal. MDN’s localhost example applies to http(s) worker script URLs. Always check location.protocol and location.href too.
No. WorkerLocation.hostname is read-only. You cannot rewrite the worker URL by assigning to hostname.
Same idea (URL hostname), different object. Inside a worker you read WorkerLocation via self.location—not the page’s Location.

Next: href

Read the full serialized worker script URL.

href 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