Skip to content

Get current domain name using JavaScript

When you use document.location, you get the protocol and the path of the current page, for example https://api.example.com/posts/add. But if you only want the domain name, in this case api.example.com, you can use the hostname property of window.location:

[sc name=”src” url=”https://stackoverflow.com/questions/11401897/get-the-current-domain-name-with-javascript-not-the-path-etc” ]

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.