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?
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?
 
    
    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.
 
    
    This worked for me.
http://tech-blog.maddyzone.com/javascript/get-current-url-javascript-jquery
$(location).attr('host');                        www.test.com:8082
$(location).attr('hostname');                    www.test.com
$(location).attr('port');                        8082
$(location).attr('protocol');                    http:
$(location).attr('pathname');                    index.php
$(location).attr('href');                        http://www.test.com:8082/index.php#tab2
$(location).attr('hash');                       #tab2
$(location).attr('search');                     ?foo=123
 
    
     
    
    Try like this.
var hostname = window.location.origin
If the URL is "http://example.com/path" then you will get  "http://example.com" as the result.
This won't work for local domains
When you have URL like "https://localhost/MyProposal/MyDir/MyTestPage.aspx" 
and your virtual directory path is "https://localhost/MyProposal/" 
In such cases, you will get "https://localhost".
 
    
     
    
    You can do this with plain js by using
location.host , same as document.location.hostname 
    
    You don't need jQuery for this, as simple javascript will suffice:
alert(document.domain);
See it in action:
console.log("Output;");  
console.log(location.hostname);
console.log(document.domain);
alert(window.location.hostname)
console.log("document.URL : "+document.URL);
console.log("document.location.href : "+document.location.href);
console.log("document.location.origin : "+document.location.origin);
console.log("document.location.hostname : "+document.location.hostname);
console.log("document.location.host : "+document.location.host);
console.log("document.location.pathname : "+document.location.pathname);
for more details click here window.location
just append "http://" before domain name to get appropriate result.
 
    
    You can use a trick, by creating a <a>-element, then setting the string to the href of that <a>-element and then you have a Location object you can get the hostname from.
You could either add a method to the String prototype:
String.prototype.toLocation = function() {
    var a = document.createElement('a');
    a.href = this;
    return a;
};
and use it like this:
"http://www.abc.com/search".toLocation().hostname
or make it a function:
function toLocation(url) {
    var a = document.createElement('a');
    a.href = url;
    return a;
};
and use it like this:
toLocation("http://www.abc.com/search").hostname
both of these will output: "www.abc.com"
If you also need the protocol, you can do something like this:
var url = "http://www.abc.com/search".toLocation();
url.protocol + "//" + url.hostname
which will output: "http://www.abc.com"
 
    
    While pure JavaScript is sufficient here, I still prefer the jQuery approach. After all, the ask was to get the hostname using jQuery.
var hostName = $(location).attr('hostname');      // www.example.com
 
    
    To get the url as well as the protocol used we can try the code below.
For example to get the domain as well as the protocol used (http/https).
https://google.com
You can use -
host = window.location.protocol+'//'+window.location.hostname+'/';
It'll return you the protocol as well as domain name. https://google.com/
 
    
    var hostname = window.location.origin
Will not work for IE. For IE support as well I would something like this:
var hostName = window.location.hostname;
var protocol = window.locatrion.protocol;
var finalUrl = protocol + '//' + hostname;
try this code below it works fine with me.
example below is getting the host and redirecting to another page.
var host = $(location).attr('host');
window.location.replace("http://"+host+"/TEST_PROJECT/INDEXINGPAGE");
