
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
- Related Articles
- How to show the domain name of the server that loaded the document with JavaScript?
- How to display the title of a document with JavaScript?
- How to display the date and time of a document when it is last modified in JavaScript?
- Differentiate between domain and domain name server and components of DNS
- Difference between Domain and Server
- How to find the number of links in a document in JavaScript?
- How to use JavaScript to replace the content of a document with JavaScript?
- How to return the number of images in a document with JavaScript?
- How to get the number of forms in a document with JavaScript?
- How to show the number of links in a document with JavaScript?
- How to Display System Variables of MySQL Server?
- How to find the href attribute of a link in a document in JavaScript?
- How to return the id of the first image in a document with JavaScript?
- How to find the name of the first form in a document with JavaScript?
- How to show the full URL of a document with JavaScript?
