Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old September 4th, 2008, 11:34 AM
Member
 
Join Date: Oct 2007
Posts: 32
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, 11:37 AM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Age: 36
Posts: 3,448
Default

you may use:

Expand|Select|Wrap|Line Numbers
  1. var hn = window.location.hostname;
Reply
  #3  
Old September 4th, 2008, 11:43 AM
Member
 
Join Date: Oct 2007
Posts: 32
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, 01:20 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Age: 36
Posts: 3,448
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, 01:15 PM
Member
 
Join Date: Oct 2007
Posts: 32
Default

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

looks a little fragile.

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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 205,248 network members.