Please help me how to get host name using html and javascript.

Please help me how to get host name using html and javascript.

Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Prasad RaveendranRank #320 · 7.8K reputationPosted May 31, 2023, 2:32 PM EDT
HTML File
Javascript file
window.addEventListener('DOMContentLoaded', function() { var hostname = window.location.hostname; var hostnameElement = document.getElementById('hostname'); hostnameElement.textContent = hostname; });In this example, we have an HTML page with an
heading and a
paragraph element. The JavaScript code listens for the DOMContentLoaded event to ensure the HTML has finished loading before attempting to access and modify the elements.
The window.location.hostname retrieves the hostname, and document.getElementById('hostname') selects the paragraph element with the id "hostname". Then, we update the text content of the paragraph element using textContent to display the hostname.
Rajkiran SwainRank #165 · 14.4K reputationPosted May 31, 2023, 7:31 AM EDT
Aravind GovindarajRank #430 · 5.8K reputationPosted May 31, 2023, 10:39 AM EDT
Please find the sample snippet
Get Client Hostname function getClientHostname() { var url = window.location.href; var hostname = new URL(url).hostname; document.getElementById("hostname").textContent = "Client Hostname: " + hostname; } Client Hostname Example Get Client HostnameRolando Puno137 reputationPosted May 31, 2023, 11:54 PM EDT
Hello Prasad..

Thanks for your quick reply..by the way im trying to used your code below then i foud out the return is localhost. i want ot get like this: DW00262236
Thank you aonce again