How to obtain Domain name via JavaScript?

JavascriptDomain Name

Javascript Problem Overview


Is there a way to obtain the domain name of the current page by using javascript in a browser (IE, FireFox, etc) ?

Javascript Solutions


Solution 1 - Javascript

If you are asking about the machine name of the host that served up the current page, you can find that with

window.location.hostname

If you are asking about the name of the machine that is displaying the page in a browser, that information is restricted by browsers for security purposes.

Solution 2 - Javascript

You can use document.domain to get domain name.

Solution 3 - Javascript

window.location.host

EDIT: If you meant the domain of the client machine, I don't think it is possible.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAshutosh NasikkarView Question on Stackoverflow
Solution 1 - JavascriptMike ClarkView Answer on Stackoverflow
Solution 2 - JavascriptBobView Answer on Stackoverflow
Solution 3 - JavascriptChetan SView Answer on Stackoverflow