JavaScript WorkerLocation host Property

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

What You’ll Learn

The host property of a WorkerLocation object returns the host part of the worker’s location. Learn self.location.host, how it relates to hostname and port, and what blob workers report—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

Inside a Web Worker, self.location is a WorkerLocation for the worker script URL. host is the authority-style host string—usually hostname plus port when the port is shown (for example localhost:8080).

MDN’s classic example: for a worker on http://localhost:8080/, location.host returns "localhost:8080".

💡
Beginner tip

Blob-worker demos often show an empty location.host because blob: URLs do not use a normal hostname. That is expected—read protocol and href together.

Understanding the host Property

A read-only instance property on WorkerLocation that returns the host part of the worker’s location as a string.

  • Value — a string (for example "localhost:8080").
  • vs hostnamehost may include the port; hostname does not.
  • Accessself.location.host or location.host 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 = location.host;
// e.g. "localhost:8080" on http://localhost:8080/

Value

A string.

Typical pattern (MDN idea)

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

⚡ Quick Reference

GoalCode / note
Read hostself.location.host
Return typeString
MDN example"localhost:8080"
Blob workersOften host === ""
Send to mainself.postMessage(location.host)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about WorkerLocation.host.

Returns
string

host[:port]

Where
in worker

self.location

Writable
no

Read-only

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN WorkerLocation.host. Labs use blob workers (host may be empty) and compare related URL parts.

📚 Getting Started

Read the host the MDN way.

Example 1 — Read location.host (MDN Idea)

On an HTTP worker URL like http://localhost:8080/, host includes the port.

JavaScript
// On http://localhost:8080/
const result = location.host; // "localhost:8080"
self.postMessage(result);
Try It Yourself

How It Works

The try-it lab posts the live blob worker’s host (often "") plus href so you can see both worlds.

Example 2 — Blob Worker Host

Blob URLs usually 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

Do not treat an empty blob host as a bug—use href when you need the full worker URL.

📈 Parts & Comparison

Compare hostname, page host, and port detection.

Example 3 — host vs hostname vs port

Three related pieces of the same URL authority.

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

How It Works

On blob workers these strings are often empty; on http://localhost:8080/ they look like the MDN sample.

Example 4 — Worker Host vs Page Host

Post the worker host; compare it with window.location.host on the main thread.

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

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

How It Works

The page host and the worker host can differ when the worker script URL is a blob: URL.

Example 5 — Does host Include a Port?

A simple check: does the host string contain :?

JavaScript
const host = location.host;
self.postMessage({
  host,
  hasPortMarker: host.indexOf(":") !== -1
});
Try It Yourself

How It Works

For production parsing prefer location.port / hostname, or the URL constructor on location.href.

🚀 Common Use Cases

  • Log which host a dedicated worker script was loaded from.
  • Branch behavior for local vs production hosts.
  • Debug whether a worker is a blob URL (empty host) or http(s).
  • Pair with hostname / port when building diagnostics.
  • Compare worker host with the page host when investigating origin issues.

🔧 How It Works

1

Worker URL

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

URL
2

WorkerLocation

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

API
3

Read host

location.host returns the host string (or "" for many blob URLs).

Read
4

Use or post

Branch logic, log diagnostics, or postMessage the value 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; often empty for blob: worker URLs.
  • Not the same object as window.location on the page.
  • Related learning: hash, Worker(), postMessage().

Universal Browser Support

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

Returns the host part of the worker's location as a string. Read it with self.location.host 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 Workers / WorkerLocation in IE10+ (prefer modern browsers)
Legacy
WorkerLocation.host Excellent

Bottom line: Read location.host inside the worker—expect an empty string for many blob: demos, and localhost:port for typical http(s) worker scripts.

Conclusion

WorkerLocation.host is the worker-side host string for the script URL—often hostname:port for http(s) workers. Remember blob workers may report an empty host, and always read it inside the worker (then post if the UI needs it).

Continue with hostname, hash, Worker(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read host only inside a worker
  • Compare with hostname and port when debugging
  • Expect empty host for many blob workers
  • Use postMessage to surface the value on the page
  • Prefer href when you need the full worker URL

❌ Don’t

  • Expect WorkerLocation on the main thread
  • Assign to location.host in a worker (read-only)
  • Confuse page window.location.host with the worker’s host
  • Assume blob demos always match MDN’s localhost example
  • Parse IPv6 hosts with naive split(":") only

Key Takeaways

Knowledge Unlocked

Five things to remember about WorkerLocation.host

The host part of the worker script URL, as a string.

5
Core concepts
⚙️02

Workers

self.location

Context
🔒03

Read-only

no assign

Rule
📄04

vs hostname

may include port

Compare
🎯05

Baseline

since Jul 2015

Status

❓ 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.
Did you know?

Default ports (80 for HTTP, 443 for HTTPS) are often omitted from host. Non-default ports like 8080 or 3002 usually appear after a colon—exactly like MDN’s localhost:8080 example.

Next: WorkerLocation.hostname

Learn how to read the hostname (no port) of a worker script URL.

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.

5 people found this page helpful