Extract domain name suffix from any url
<p>I have a url in string format like this :   </p>

<pre><code>str="http://code.google.com"

and some other like str="http://sub.google.co.in"      
</code></pre>

<p>i want to extract google.com from first one, and google.co.in from second string .  </p>

<p>what i did is :  </p>

<pre><code>var a, d, i, ind, j, till, total;

a = document.createElement('a');

a.href = "http://www.wv.sdf.sdf.sd.ds..google.co.in";

d = "";

if (a.host.substr(0, 4) === "www.") {
  d = a.host.replace("www.", "");
} else {
  d = a.host;
}

till = d.indexOf(".com");

total = 0;

for (i in d) {
  if (i === till) {
    break;
  }
  if (d[i] === ".") {
    total++;
  }
}

j = 1;

while (j &lt; total) {
  ind = d.indexOf(".");
  d = d.substr(ind + 1, d.length);
  j++;
}

alert(d);
</code></pre>

<p>My code works but it works only for ".com"  , it doesnt work for others like ".co.in","co.uk" till i specify them manually , Can anyone tell me the solution for this ? I dont mind even i need to change the full code, but it should work . Thanks  </p>
