Get domain from URL

Get com,co.uk,us ... from URL

Page 1 of 1

8 Replies - 6771 Views - Last Post: 10 February 2010 - 12:45 PM

#1 JohnWaters  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 138
  • Joined: 28-November 08

Get domain from URL

Posted 04 August 2009 - 11:53 AM

Hi Guys,

How do i get the domain from a URL ?

Some domains are ( com co.uk us gov ie net org ) so all I need is the domain itself.

So far all i can get is the hostname and domain...
   var url = "http://www.dreamincode.net";
   return url.match(/:\/\/(.[^/]+)/)[1];



Any help is welcome :)

Is This A Good Question/Topic? 0
  • +

Replies To: Get domain from URL

#2 arthurakay  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 17-February 09

Re: Get domain from URL

Posted 04 August 2009 - 11:57 AM

like this?
var url = "http://www.dreamincode.net";
alert(url.match(/:\/\/(.[^/]+)/)[1].split('.')[1]);


Was This Post Helpful? 0
  • +
  • -

#3 JohnWaters  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 138
  • Joined: 28-November 08

Re: Get domain from URL

Posted 04 August 2009 - 12:07 PM

This now only gives my the host name ... dreamincode.

I actually need the com not forgetting that some domains have an additional . eg. co.uk

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 arthurakay  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 17-February 09

Re: Get domain from URL

Posted 04 August 2009 - 12:14 PM

Ah, sorry. I misunderstood.

How about this:
var url = "http://www.domain.co.uk";
var parsedUrl = url.match(/:\/\/(.[^/]+)/)[1].split('.');
var domain = '';

for (var i=2; i<parsedUrl.length; i++) {
	if (i != 2) { domain += '.'; }

	domain += parsedUrl[i];
}
alert(domain);


Was This Post Helpful? 0
  • +
  • -

#5 JohnWaters  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 138
  • Joined: 28-November 08

Re: Get domain from URL

Posted 04 August 2009 - 12:36 PM

Although this works for a standard URL , I still have an issue with URLs containing sub domains and URLs not containing www.

I appreciate the help : )
Was This Post Helpful? 0
  • +
  • -

#6 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: Get domain from URL

Posted 04 August 2009 - 01:42 PM

how about this?: (enjoy the psuedocode :P)

count the number of periods in the url between the two main slashes (IE: http://www.dreamincode.net/

Then split it into it's separate parts by the periods

Keep an array of known 2 part domains (ie: co.uk), having the domain as the key and the number of periods as the value (ie: array("co.uk", 2))

then select either the last one or two to get the domain, putting it back together with a period.

It's similar to the code posted here
Was This Post Helpful? 0
  • +
  • -

#7 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: Get domain from URL

Posted 04 August 2009 - 05:16 PM

Is .co.uk the only domain suffix with two periods? if it is then you could just make a separate case for it.
Was This Post Helpful? 0
  • +
  • -

#8 kewlkreator  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 38
  • View blog
  • Posts: 1,065
  • Joined: 25-March 09

Re: Get domain from URL

Posted 08 August 2009 - 08:15 PM

Naw, there's a bunch like .co.nr or .co.cc, etc.

BTW: there's a LOT of those. No use making cases for all of them.
Was This Post Helpful? 0
  • +
  • -

#9 JohnWaters  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 138
  • Joined: 28-November 08

Re: Get domain from URL

Posted 10 February 2010 - 12:45 PM

Hey guys. I've recently ended back up with this same problem and still cant find a final solution. Here is some more information which might help..

Im using the domain lowering technique (setting document.domain), and want get retrieve the domain from the URL rather than hard coding it.

So im having problem using normal regular expression because some domains have an extra level such as co.uk (could be any 2 level domain).

Example URL 1: www.foo.com > domain would be foo.com
Example URL 2: www.bar.foo.com > domain would be foo.com
Example URL 3: www.foo.co.uk > domain SHOULD be foo.co.uk
Example URL 4: www.bar.foo.co.uk > domain SHOULD be foo.co.uk

So when I try the regular expression on URL 1 and 2 and get the correct domain, but for URL 3 and URL 4 I end up getting only co.uk.

Does anyone have a solution to this ? Any help is greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1