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
Call
What it does
location.hostname
Primary signature.
Built into Web Workers. Read self.location.hostname (or location.hostname) inside a worker — no import required.
Library declarations
JavaScript
location.hostname
Parameters
Parameter
Description
Worker context
Required. self.location is a WorkerLocation
Return Value
Return Value
Description
*
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);
hostname: ""
host: ""
port: ""
(on http://localhost:8080 → hostname "localhost", host "localhost:8080", port "8080")
How It Works
Three pieces. Read hostname, host, and port from the same location.
hostname is bare. No port number in hostname.
host combines. When a port is shown, host looks like hostname:port.
Pick the right one. Need name only? Use hostname. Need authority? Use host.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
WorkerLocation.hostnameExcellent
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.