HTML DOM domain Property


The HTML DOM domain property is used for getting the domain name of the server on which the document is being currently executing. It returns domain name in the form of string and the value can be null if the domain is not identified.

Syntax

Following is the syntax for domain property −

document.domain

Example

Let us look at an example for the HTML DOM domain property −

<!DOCTYPE html>
<html>
<style>
<h1>domain property example</h1>
<p>Get the domain name property value by clicking the below button </p>
<button onclick="getDomain()">Get domain</button>
<p id="Sample"></p>
<script>
   function getDomain() {
      var doc = document.domain;
      document.getElementById("Sample").innerHTML ="The domain name of the server running
      this document is "+doc;
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the “Get domain” button −

We have first created a “Get domain” button that will execute the getDomain() method when clicked by the user −

<button onclick="getDomain()">Get domain</button>

The getDomain() function gets the document’s domain property and assigns its value to variable doc. This variable doc is then displayed in the paragraph that has id “Sample” associated with it and used the innerHTML property to display the intended text −

function getDomain() {
   var doc = document.domain;
   document.getElementById("Sample").innerHTML ="The domain name of the server running this document is "+doc;
}

Updated on: 08-Aug-2019

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements