I have domain name for eq.
1) http://www.abc.com/search
2) http://go.abc.com/work
I get only domain name from the above URL
Output like
1) http://www.abc.com/
2) http://go.abc.com/
how can I do?
This question is related to
jquery
You can leverage the browser's URL parser using an <a>
element:
var hostname = $('<a>').prop('href', url).prop('hostname');
or without jQuery:
var a = document.createElement('a');
a.href = url;
var hostname = a.hostname;
(This trick is particularly useful for resolving paths relative to the current page.)
Use the following function:
function get_hostname(url) {
var m = url.match(/^http:\/\/[^/]+/);
return m ? m[0] : null;
}
Use it like this:
get_hostname("http://example.com/path");
This will return http://example.com/
as in your example output.
If you are only trying the get the hostname of the current page, use document.location.hostname
.