JavaScript document.domain: Legacy Domain API and Alternatives

Deprecated: Avoid document.domain in new projects. Use location.hostname when you only need the current hostname, and use controlled cross-origin messaging rather than weakening origin checks.

document.domain historically exposed the domain portion of the document origin and also allowed pages on related subdomains to alter their effective domain. That origin-relaxation behavior is the main reason the API is deprecated.

const hostname = location.hostname;
console.log(hostname);
Table of Contents

Reading document.domain in Legacy Code Top ↑

Older code may still contain:

const domain = document.domain;
Modern hostname for this page:

Use location.hostname for the Current Hostname Top ↑

If the goal is only to identify the host on which the script is running, use:

const hostname = location.hostname;

For more address properties, see document.URL and location.

Why Setting document.domain Is Deprecated Top ↑

Legacy applications sometimes set both subdomains to a shared parent domain:

// Legacy pattern — do not use in new code
document.domain = "example.com";

This weakens the normal same-origin boundary and complicates modern browser isolation. It can also fail in sandboxed or origin-isolated contexts. Do not add this pattern to new applications.

Communicate Between Origins with postMessage() Top ↑

When two windows or frames from different origins must exchange controlled data, use postMessage() with an explicit target origin and validate the sender.

otherWindow.postMessage({ type: "status" }, "https://app.example.com");

window.addEventListener("message", (event) => {
  if (event.origin !== "https://app.example.com") return;
  console.log(event.data);
});

location.host, hostname and origin Top ↑

PropertyExample result
location.hostnamewww.example.com
location.hostwww.example.com:8080 when a port is present
location.originhttps://www.example.com
document Object URL of a Page


Subscribe to our YouTube Channel here



plus2net.com







danish

19-03-2015

Thanks very much solve the problem :) God bless you



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer