get domain name from url 
September 4th, 2008, 11:34 AM
| | Member | | Join Date: Oct 2007
Posts: 39
| | |
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.
| 
September 4th, 2008, 11:37 AM
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,102
Provided Answers: 1 | | | re: get domain name from url
you may use: - var hn = window.location.hostname;
| 
September 4th, 2008, 11:43 AM
| | Member | | Join Date: Oct 2007
Posts: 39
| | | re: get domain name from url
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.
| 
September 4th, 2008, 01:20 PM
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,102
Provided Answers: 1 | | | re: get domain name from url
hmmm ... that's another question ... try a regExp like the following: - var url = 'http://bytes.com/forum/newthread.php';
-
-
function get_hostname_from_url(url) {
-
return url.match(/:\/\/(.[^/]+)/)[1];
-
}
-
-
alert(get_hostname_from_url(url));
kind regards
| 
September 5th, 2008, 01:15 PM
| | Member | | Join Date: Oct 2007
Posts: 39
| | | re: get domain name from url
k..thanks..i'll try that
| 
September 8th, 2008, 07:20 AM
|  | Expert | | Join Date: Jun 2007 Location: Urbana IL
Posts: 410
| | | re: get domain name from url
looks a little fragile.
try url.split(/\/+/g)[1];
|  | | | | /bytes/about
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 225,698 network members.
|