Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,832 members, 7,810,201 topics. Date: Friday, 26 April 2024 at 11:31 PM

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

Nairaland Forum / Science/Technology / Programming / How Do I Use Javascript To Get The Domain Name From A Link (1052 Views)

Please, Help Me Out With A Link To Download Laravel 5.3 Pdf Tutorials / Lets Start A Real Web-deveopment Course Here Html+css+javascript+php+mysql / Were Can I Download Free Video Tutorials On Javascript Or Ruby (2) (3) (4)

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

Crazy Fly Bird New Game - DOWNLOAD NOW - Review And Win / App Builders Needed / How To Promote Your Mobile Application Download Online

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 12
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.