Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #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,369
Default

you may use:

[CODE=javascript]var hn = window.location.hostname;[/code]
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,369
Default

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

[CODE=javascript]var url = 'http://bytes.com/forum/newthread.php';

function get_hostname_from_url(url) {
return url.match(/:\/\/(.[^/]+)/)[1];
}

alert(get_hostname_from_url(url));[/code]
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, home of javascript.
Age: 27
Posts: 356
Default

looks a little fragile.

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

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles