Ganesh Babu
Ganesh Babu

Reputation: 3670

How to retrieve domain name via javascript?

When I used the below script in www.example.com/about-us/user , it showed entire url

<script type="text/javascript">
     alert(document.URL);
</script>

I want to get only the domain name in here,say example.com .. Is there any javascript inbuilt function for retrieving domain name?

Upvotes: 1

Views: 438

Answers (4)

Bindiya Patoliya
Bindiya Patoliya

Reputation: 2764

Use

alert(window.location.host)

Upvotes: 0

Bj&#246;rn
Bj&#246;rn

Reputation: 29381

alert(location.hostname);

or

alert(document.domain);

Upvotes: 0

slash197
slash197

Reputation: 9034

Have you tried this:

alert(document.domain);

Upvotes: 1

Jonas Grumann
Jonas Grumann

Reputation: 10776

It's as simple as:

 alert(document.domain);

Upvotes: 3

Related Questions