0

I bought two domain name on freenom.com and I have an app on Firebase.

I am trying to know which domain as been typed to land on my page.

I tried :

location.hostname
document.location
document.location.href

But they all return the Firebase url. Does anyone knows how I could get it?

Edit: I suspect that my problem comes from Firebase or Freename, could they block me from getting the domain name ?

Antoine Grandchamp
  • 7,195
  • 3
  • 28
  • 34
  • Possible duplicate of [How to extract the hostname portion of a URL in JavaScript](http://stackoverflow.com/questions/1368264/how-to-extract-the-hostname-portion-of-a-url-in-javascript) – Jonathan Feb 23 '17 at 18:48
  • @Antoine: as you can see both the comment and first answer point to using `location.hostname`. If that doesn't work, please share the [minimal code that reproduces the problem](http://stackoverflow.com/help/mcve). Keep in mind that many thousands of web sites depend on using `location.hostname`, so your problem is likely quite specific to what you are doing. For example: if you can share the URL where you are testing, we could look at the same thing you see. – Frank van Puffelen Feb 23 '17 at 18:59
  • I added a link in my question. – Antoine Grandchamp Feb 23 '17 at 19:01

3 Answers3

0

You can do the domain like this. It ain't clean, but it works

  function getDomain() {
    var domain = window.location.hostname;
    var parts = domain.split('.').reverse();
    var cnt = parts.length;
    if (cnt >= 3) {
      if (parts[1].match(/^(com|edu|gov|net|mil|org|nom|co|name|io|info|biz)$/i)) {
          return parts[2] + '.' + parts[1] + '.' + parts[0];
      }
    }
    return parts[1] + '.' + parts[0];
  }
Sean Kwon
  • 907
  • 6
  • 12
  • It does not work for me. Try this [link](http://hotpotato.ml/) to see what my problem is. – Antoine Grandchamp Feb 23 '17 at 19:15
  • no, it doesn't work because your code is being called from an iframe. So the iframe is sourced from w3 schools so it's hostname is w3 schools. Iframes tend to be tricky... The function works, it just's calling itself from a different source. – Sean Kwon Feb 23 '17 at 19:17
0

domainName = window.location.hostname.split('.')[0]; //ie... google
console.log(domainName);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

enter image description here

Donald Powell
  • 744
  • 5
  • 10
  • In your case with Firebase... Reading http://stackoverflow.com/questions/18004008/how-can-you-get-a-url-for-every-piece-of-data may help. – Donald Powell Feb 23 '17 at 19:40
0

Try This ->

window.location.hostname