Use following javascript function to get domain name from URL.
function fnGetDomain(url) {
return url.match(/:\/\/(.[^/]+)/)[1];
}
For ex: <script language="javascript">fnGetDomain("http://mail.google.com/mail/?source=navclient-ff");</script> will return "mail.google.com"
Subscribe to:
Post Comments (Atom)
very good function, thank you
ReplyDeletei added a little tweak to remove the 'www.' part
function fnGetDomain(url) {
return (url.match(/:\/\/(.[^/]+)/)[1]).replace('www.','');
}
function getDomain(url)
ReplyDelete{
return url.match(/:\/\/(www\.)?(.[^/:]+)/)[2];
}
Excellent function utility! Thanks for the help :)
ReplyDeleteHey, the result isn't domain, it's host of a url.
ReplyDeletefor url (http://mail.google.com/mail/?xxxxx)
its host is: mail.google.com
but the domain is: google.com
and others:
http://www.google.co.ck/#hl=en&source=hp&biw=1440&bih=703&q=hello&aq=f&aqi=g10&aql=&oq=&fp=3ab7f9268f5901b
its host is: www.google.co.ck
but the domain is: google.co.ck
Black.Lee
i have to use a website as a input.so i have to cluster the webpages for pattern analysis, so have to convert a web url into a number.... can u tell me how to extract the webpage info from url and convert into integer in JAVASCRIPT.. mail me at aloksalian@yahoo.com
ReplyDeleteThanx a lot.
ReplyDeleteThis solution take into account also the optional digit after the "www" (e.g. http://www2.ohchr.org/english):
ReplyDeletefunction getDomain(url) {
return url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/)[2];
}
Thanks Nilesh,
ReplyDeleteI am new to Javascript and I was looking for this. And found on your blog. Thanks again.
You should really update the original post. It's much better than having to read the comments.
ReplyDeleteBut how to consider Black Lee's comment ?? Any help ?
ReplyDelete