469,291 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,291 developers. It's quick & easy.

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.
Sep 4 '08 #1
5 41406
gits
5,390 Expert Mod 4TB
you may use:

Expand|Select|Wrap|Line Numbers
  1. var hn = window.location.hostname;
Sep 4 '08 #2
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.
Sep 4 '08 #3
gits
5,390 Expert Mod 4TB
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
Sep 4 '08 #4
k..thanks..i'll try that
Sep 5 '08 #5
rnd me
427 Expert 256MB
looks a little fragile.

try url.split(/\/+/g)[1];
Sep 8 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jordan | last post: by
3 posts views Thread by Bruno | last post: by
4 posts views Thread by Bruno | last post: by
16 posts views Thread by Michael | last post: by
6 posts views Thread by Anders K. Olsen | last post: by
6 posts views Thread by Adam Tibi | last post: by
6 posts views Thread by Spyder | last post: by
8 posts views Thread by None | last post: by
9 posts views Thread by Sam | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.