1function extractDomain(url, y) {
2 var domain;
3
4 //remove protocol
5 if (url.indexOf("://") > -1) { domain = url.split('/')[2]; }
6 else { domain = url.split('/')[0]; }
7
8 //remove port number
9 domain = domain.split(':')[0];
10
11
12 if (y == 1) {
13 d = splitHostname(domain);
14 if (d.subdomain != "www" && d.subdomain != "") {
15 return d.subdomain;
16 }
17 else {
18 return d.domain;
19 }
20 } else {
21 return domain;
22 }
23}
24
25function addhttp(url) {
26 if (!/^(f|ht)tps?:\/\//i.test(url)) {
27 url = "http://" + url;
28 }
29 return url;
30}
31
32function splitHostname(h) {
33 var result = {};
34 var urlParts = new RegExp('([a-z\-0-9]{2,63})\.([a-z\.]{2,5})$').exec(h);
35 result.domain = urlParts[1];
36 result.type = urlParts[2];
37 result.subdomain = h.replace(result.domain + '.' + result.type, '').slice(0, -1);;
38 return result;
39}