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
Fundamentals
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.
Concept
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.
Access — self.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).
Foundation
📝 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);
Compare
⚖️ hostname vs Related Ideas
Idea
Meaning
location.hostname
Hostname only (no port)
location.host
Hostname + port when applicable
location.port
Port string alone
location.href
Full serialized worker URL
window.location.hostname
Hostname of the page URL (main thread)
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Read hostname
self.location.hostname
Return type
String
MDN example
"localhost"
Blob workers
Often hostname === ""
Send to main
self.postMessage(location.hostname)
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 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
Hands-On
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);
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerWorkers / WorkerLocation in IE10+ (prefer modern browsers)
Legacy
WorkerLocation.hostnameExcellent
Bottom line: Read location.hostname inside the worker—expect an empty string for many blob: demos, and localhost for typical http://localhost:… worker scripts.
Wrap Up
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.
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
Summary
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
🔗01
Returns
name string
API
⚙️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.