How to get Domain name from URL using jquery..?

Jquery

Jquery Problem Overview


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?

Jquery Solutions


Solution 1 - Jquery

In a browser

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.)

Outside of a browser (and probably more efficiently):

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.

Hostname of the current page

If you are only trying the get the hostname of the current page, use document.location.hostname.

Solution 2 - Jquery

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

Solution 3 - Jquery

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".

Solution 4 - Jquery

You can do this with plain js by using

  1. location.host , same as document.location.hostname

  2. document.domain Not recommended

Solution 5 - Jquery

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][1]

just append "http://" before domain name to get appropriate result.

[1]: https://developer.mozilla.org/en-US/docs/Web/API/Window/location "window.location"

Solution 6 - Jquery

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"

Solution 7 - Jquery

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

Solution 8 - Jquery

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/

Solution 9 - Jquery

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;

Solution 10 - Jquery

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");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbhishek B.View Question on Stackoverflow
Solution 1 - JqueryArnaud Le BlancView Answer on Stackoverflow
Solution 2 - JqueryAdam MendozaView Answer on Stackoverflow
Solution 3 - JquerysudhAnsu63View Answer on Stackoverflow
Solution 4 - JqueryClyde LoboView Answer on Stackoverflow
Solution 5 - JqueryDevendra ChhaiyaView Answer on Stackoverflow
Solution 6 - JquerySindre SorhusView Answer on Stackoverflow
Solution 7 - JqueryTaylor CoxView Answer on Stackoverflow
Solution 8 - JquerylazycipherView Answer on Stackoverflow
Solution 9 - JqueryPrateek Kumar JauhariView Answer on Stackoverflow
Solution 10 - JqueryHermesView Answer on Stackoverflow