Connecting Tech Pros Worldwide Help | Site Map

get domain name from url

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 4th, 2008, 10:34 AM
Member
 
Join Date: Oct 2007
Posts: 39
Default get domain name from url

i need to get the domain name from the url in javascript.

for eg if the url is "http://bytes.com/forum/newthread.php", i need to get bytes.com alone.

any quick help will be appreciated.
Reply
  #2  
Old September 4th, 2008, 10:37 AM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Age: 37
Posts: 3,780
Default

you may use:

Expand|Select|Wrap|Line Numbers
  1. var hn = window.location.hostname;
Reply
  #3  
Old September 4th, 2008, 10:43 AM
Member
 
Join Date: Oct 2007
Posts: 39
Default

Thanks for the quick reply....this returns the domain name of the current url...
i need this to be a utility function where i can pass a url and get the domain name.
Reply
  #4  
Old September 4th, 2008, 12:20 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Age: 37
Posts: 3,780
Default

hmmm ... that's another question ... try a regExp like the following:

Expand|Select|Wrap|Line Numbers
  1. var url = 'http://bytes.com/forum/newthread.php';
  2.  
  3. function get_hostname_from_url(url) {
  4.     return url.match(/:\/\/(.[^/]+)/)[1];
  5. }
  6.  
  7. alert(get_hostname_from_url(url));
kind regards
Reply
  #5  
Old September 5th, 2008, 12:15 PM
Member
 
Join Date: Oct 2007
Posts: 39
Default

k..thanks..i'll try that
Reply
  #6  
Old September 8th, 2008, 06:20 AM
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 402
Default

looks a little fragile.

try url.split(/\/+/g)[1];
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.