JavaScript WorkerLocation hostname Property

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

What You’ll Learn

The hostname property of a WorkerLocation object returns the hostname part of the worker’s location (no port). Learn self.location.hostname, how it differs from host, 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. hostname is just the name part—for example localhost—without any port number.

MDN’s classic example: for a worker on http://localhost:8080/, location.hostname returns "localhost" (not "localhost:8080").

💡
Beginner tip

Need the port too? Use host (or port). Blob-worker demos often show an empty hostname—that is normal for blob: URLs.

Understanding the hostname Property

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

  • Value — a string (for example "localhost").
  • No port — unlike host, the port is not included.
  • Accessself.location.hostname or location.hostname 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.hostname;
// e.g. "localhost" on http://localhost:8080/

Value

A string.

Typical pattern (MDN idea)

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

⚡ Quick Reference

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

🔍 At a Glance

Four facts to remember about WorkerLocation.hostname.

Returns
string

Name only

Where
in worker

self.location

Writable
no

Read-only

Baseline
widely

Since Jul 2015

Examples Gallery

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

📚 Getting Started

Read the hostname the MDN way.

Example 1 — Read location.hostname (MDN Idea)

On http://localhost:8080/, hostname is just localhost.

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

How It Works

The try-it lab posts the live blob worker’s hostname (often "") plus the MDN expected value for comparison.

Example 2 — Blob Worker Hostname

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

An empty blob hostname is expected—use href when you need the full worker URL.

📈 Parts & Comparison

Compare host, page hostname, and local checks.

Example 3 — hostname vs host vs port

Three related pieces of the same URL authority.

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

How It Works

hostname never includes the port; host may. On blob workers these strings are often empty.

Example 4 — Worker Hostname vs Page Hostname

Post the worker hostname; compare it with window.location.hostname.

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

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

How It Works

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

Example 5 — Is It localhost?

A simple equality check against a known hostname.

JavaScript
const name = location.hostname;
self.postMessage({
  hostname: name,
  isLocalhost: name === "localhost"
});
Try It Yourself

How It Works

Useful for branching local vs production behavior when the worker is loaded from an http(s) script URL.

🚀 Common Use Cases

  • Detect local vs production hostnames inside a worker.
  • Log which hostname a dedicated worker script was loaded from.
  • Debug blob workers (empty hostname) vs http(s) workers.
  • Pair with host / port in diagnostics.
  • Compare worker hostname with the page hostname when investigating origins.

🔧 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 hostname

location.hostname returns the name 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.
  • Does not include the port—see host.
  • Related learning: host, hash, Worker().

Universal Browser Support

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

Returns the hostname part of the worker's location as a string (no port). Read it with self.location.hostname 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.hostname Excellent

Bottom line: Read location.hostname inside the worker—expect an empty string for many blob: demos, and localhost for typical http://localhost:… worker scripts.

Conclusion

WorkerLocation.hostname is the worker-side hostname string for the script URL—name only, no port. Remember blob workers may report an empty hostname, and always read it inside the worker.

Continue with href, host, hash, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read hostname only inside a worker
  • Use host when you also need the port
  • Expect empty hostname 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.hostname in a worker (read-only)
  • Confuse page window.location.hostname with the worker’s
  • Assume blob demos always match MDN’s localhost example
  • Parse ports out of hostname—there are none

Key Takeaways

Knowledge Unlocked

Five things to remember about WorkerLocation.hostname

The hostname of the worker script URL, as a string (no port).

5
Core concepts
⚙️02

Workers

self.location

Context
🔒03

Read-only

no assign

Rule
📄04

vs host

no port here

Compare
🎯05

Baseline

since Jul 2015

Status

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

On http://localhost:8080/, hostname is "localhost" while host is "localhost:8080". Same machine, two different strings—pick the one that matches whether you need the port.

Next: WorkerLocation.href

Learn how to 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.

5 people found this page helpful