How to display the domain of the server that loaded a document in JavaScript?

In this following article we are going to learn how to display the domain of the server that loaded a document in JavaScript.

To display the domain of the server, we use domain property of the document interface, which returns the domain part of the current document. The domain property will return NULL if the document was created in memory. Attempting to set the domain property of the document interface results in SecurityError.

To understand better, let's look into the syntax and usage of domain property with suitable examples in JavaScript.

Using document.domain Property

We can get the domain name of the loaded server by using the domain property of JavaScript.

Syntax

document.domain

The domain property returns the domain name of the server. If the document was created in the memory, then the domain property returns null.

Example 1

In the following example, we used domain property to get the domain name of the loaded server.

<html>
<body>
   <p id="domain"></p>
   <script>
      document.getElementById("domain").innerHTML = 'The domain name is: ' + document.domain;
   </script>
</body>
</html>
The domain name is: tutorialspoint.com

Example 2

The following is a complete HTML document to display the domain name using document.domain.

<!DOCTYPE html>
<html>
<head>
   <title>Display Domain using document.domain</title>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="text-align: center">
   <h3>Display Domain using document.domain</h3>
   <p id='domain'></p>
   <script>
      document.getElementById('domain').innerHTML = 'The domain name is: ' + document.domain;
   </script>
</body>
</html>
The domain name is: tutorialspoint.com

Using location.hostname Property

We can get the domain name of the loaded server by using the location hostname property of JavaScript. This property is more commonly used than document.domain.

Syntax

document.location.hostname
// or simply
location.hostname

The domain name which is in the form of a string is returned.

Example

The following example demonstrates how to display the domain name using location.hostname.

<!DOCTYPE html>
<html>
<head>
   <title>Display Domain using location.hostname</title>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="text-align: center">
   <h3>Display Domain using location.hostname</h3>
   <p id='domain'></p>
   <script>
      document.getElementById('domain').innerHTML = 'The hostname is: ' + location.hostname;
   </script>
</body>
</html>
The hostname is: tutorialspoint.com

Comparison

Property Usage Returns Note
document.domain Legacy method Domain name Returns null for documents created in memory
location.hostname Modern approach Hostname More reliable and commonly used

Practical Example

Here's a practical example that shows both methods together:

<!DOCTYPE html>
<html>
<head>
   <title>Domain Detection Methods</title>
</head>
<body>
   <h3>Domain Detection Methods</h3>
   <div id="results"></div>
   <script>
      let results = document.getElementById('results');
      results.innerHTML = `
         <p><strong>document.domain:</strong> ${document.domain}</p>
         <p><strong>location.hostname:</strong> ${location.hostname}</p>
         <p><strong>Full URL:</strong> ${location.href}</p>
      `;
   </script>
</body>
</html>
document.domain: tutorialspoint.com
location.hostname: tutorialspoint.com
Full URL: https://tutorialspoint.com/javascript/how-to-display-domain-server.html

Conclusion

Both document.domain and location.hostname can retrieve the server domain, but location.hostname is the preferred modern approach. Use these properties to identify the current domain for security checks or dynamic content loading.

Updated on: 2026-03-15T23:18:59+05:30

561 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements