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 domain property

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

Syntax

The following is the syntax of the domain property of JavaScript −

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 exmaple, 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>

On executing the above code, the following output is generated.

Example 2

The following is an example program to display the domain name for the path: https://cloud.google.com/run/docs/mapping-custom-domains using the document.domain.

<!DOCTYPE html>
<html>
<head>
   <title>To display the domain of the server that loaded a document in JavaScript</title>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="text-align : center">
   <h3>To display the domain of the server that loaded a document in JavaScript</h3>
   <p id='domain'></p>
   <script>
      document.getElementById('domain').innerHTML = 'The domain name is : '+document.domain;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Using location.hostname property

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

Syntax

The following is the syntax of the location.hostname property of JavaScript −

document.location.hostname;

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

Example

The following is an example program to display the domain name for the path: https://cloud.google.com/run/docs/mapping-custom-domains using the document.location.hostname.

<!DOCTYPE html>
<html>
<head>
   <title>To display the domain of the server that loaded a document in JavaScript</title>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="text-align : center">
   <h3>To display the domain of the server that loaded a document in JavaScript</h3>
   <p id='domain'></p>
   <script>
      document.getElementById('domain').innerHTML = 'The domain name is : '+document.location.hostname;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Updated on: 08-Dec-2022

361 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements