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"
Our highest priority is to satisfy the customer through early and continuous delivery of valuable and working software.
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];
}
Thank you. Great Code for getting domain.
DeleteExcellent 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 ?
ReplyDeleteThanks was very useful.
ReplyDeleteGuys i am trying to change this url listtop10.com to www.listtop10.com with replace method but there is no effect nor any error, can please someone help me.
ReplyDeleteThanks in advance
just what I was after thanks
ReplyDeleteThanks for this post, as it was very helpful. I have a question in regards to parsing out everything after the "/" in the domain. Example if I have this domain http://www.google.com/patents/US20120211565. Is there a way to parse out the US20120211565 using a similar script like the one above?
ReplyDeleteThanks for you assistance in advanced
I have added some errors control
ReplyDeletefunction getDomain(url) {
var s = url.substr(0, 8), d;
try{
if(s.search('http://')<0 && s.search('https://')<0 && s.search('ftp://')<0){s="http://"+url;}else{s=url;}
d = s.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/)[2];}
catch(e){
d = "" ;}
return d ;
}
thanks for you assistance in advanced
ReplyDeletethanks for your post and thank you tayou fabrice it was really work full for me
ReplyDeletevery nice faction thanks
ReplyDeleteif(s.search('http://')<0 && s.search('https://')<0 && s.search('ftp://')<0){s="http://"+url;}else{s=url;}
ReplyDeleted = s.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/)[2];}
catch(e){
d = "" ;}
return d ;
}
thanks for your post and thank you tayou fabrice it was really work full for me