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:

var domain = window.location.hostname;

(source)

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.