By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

get domain name from url

P: 40
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
Share this Question
Share on Google+
5 Replies


gits
Expert Mod 5K+
P: 5,390
you may use:

Expand|Select|Wrap|Line Numbers
  1. var hn = window.location.hostname;
Sep 4 '08 #2

P: 40
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
Expert Mod 5K+
P: 5,390
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

P: 40
k..thanks..i'll try that
Sep 5 '08 #5

rnd me
Expert 100+
P: 427
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.