Skip to content

What should I use instead of document.domain to get the domain?

It appears that document.domain is deprecated:

document domain is deprecated

mdn web docs also say:

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible;

However, I don’t seem to find anywhere what can I use instead, to get the domain. I don’t need to set, only get.

Other articles I read about the deprecation only mention about the alternative for the set problem, but they don’t mention a solution/alternative for those who just want to get the domain.



I believe this is the best alternative I could find: location.hostname

Seems to contain only the domain and subdomain, without the port, similar to the result of document.domain.

User contributions licensed under: CC BY-SA
9 People found this is helpful