₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,331,151 members, 8,448,868 topics. Date: Tuesday, 21 July 2026 at 12:41 AM

Toggle theme

How Do I Use Javascript To Get The Domain Name From A Link - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingHow Do I Use Javascript To Get The Domain Name From A Link (1231 Views)

1 Reply (Go Down)

How Do I Use Javascript To Get The Domain Name From A Link by Nobody: 2:01pm On Jul 12, 2015
Given a link: http://example.com/hhhhhhhh/hhhh/ffff or http://www.example.com/hhhhhhhh/hhhh/ffff

How do I get a javascript code to print the "example.com(or www.example.com)" from the above url.

How do I also save it to in a variable.
Re: How Do I Use Javascript To Get The Domain Name From A Link by bomsy1(m): 3:35pm On Jul 12, 2015
@Charlene, you can use this


function getHost(urlStr) {
var hostResult = document.createElement("a"wink;
hostResult.href = urlStr;
return hostResult.host;
};

so for...

var hostnm = getHost("https://www.nairaland.com/abc/123");

"hostnm" should produce "www.nairaland.com".

oh..you might need to validate your url string before using the function, and i tested it on firefox... you can confirm if it also works on IE (not so sure though..)

Hope that helps....
Re: How Do I Use Javascript To Get The Domain Name From A Link by Nobody: 4:52pm On Jul 12, 2015
bomsy1:
@Charlene, you can use this


function getHost(urlStr) {
var hostResult = document.createElement("a"wink;
hostResult.href = urlStr;
return hostResult.host;
};

so for...

var hostnm = getHost("https://www.nairaland.com/abc/123");

"hostnm" should produce "www.nairaland.com".

oh..you might need to validate your url string before using the function, and i tested it on firefox... you can confirm if it also works on IE (not so sure though..)

Hope that helps....
How do I validate the url string before using the function?
Re: How Do I Use Javascript To Get The Domain Name From A Link by Craigston: 6:20pm On Jul 12, 2015
Charleen94:
Given a link: http://example.com/hhhhhhhh/hhhh/ffff or http://www.example.com/hhhhhhhh/hhhh/ffff

How do I get a javascript code to print the "example.com(or www.example.com)" from the above url.

How do I also save it to in a variable.

If you're using it on a web page, the location object is already provided with a host property which contains the current page's host name.


var domainName = location.host;



/* if you want ignore the port (eg, 127.0.0.1:80, return a string containing only values before the colon*/


hasPort = domainName.indexOf(':');
if(hasPort != -1)
{
domainName =
domainName.substring(0, hasPort);
}
Re: How Do I Use Javascript To Get The Domain Name From A Link by Craigston: 6:29pm On Jul 12, 2015
Charleen94:
How do I validate the url string before using the function?
How do you want to validate it?
Re: How Do I Use Javascript To Get The Domain Name From A Link by Jeffflo: 8:58pm On Jul 12, 2015
Whoop! MetalWorks LLC is offering a PRICE SLASH ON all WEB DESIGN AND DEVELOPMENT JOBS for prospective and existing customers. What does this mean for me you ask? Right you are. What is means is that from the beginning of this month, your photography blogs, e-commerce sites, fora et.al may all be set up with the minimum of fuss with our crack team of programmers. As additional incentive, the option of PAYMENT IN INSTALLMENTS is available upon agreement. Great huh? Of course you think so, you bargain-hunter you. So yeah, call Toby on 07033503357 to set up your free consultation today. You'll be glad you did.
1 Reply

Please, Help Me Out With A Link To Download Laravel 5.3 Pdf TutorialsLets Start A Real Web-deveopment Course Here Html+css+javascript+php+mysqlWere Can I Download Free Video Tutorials On Javascript Or Ruby234

ISP BusinessGet Unlimited Tutorials On Programming Through Your MailBuild A Career In Mobile App Development