10 examples of 'js get domain from url' in JavaScript

Every line of 'js get domain from url' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
76function getDomainOfUrl(url) {
77 var domain = url.substring(url.indexOf('/')+2);
78 var slashIndex = domain.indexOf("/");
79 return (slashIndex !== -1 ? domain.substr(0,slashIndex) : domain);
80}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

23function getDomain(url){
24 var t=url.split('://')[1];
25 return t.split('/')[0];
26}
32public static getDomain(url: string): string {
33 let urlParts = url.split('/');
34 return urlParts[(urlParts.length - 2)];
35}
151function getDomain(referer) {
152 if (referer) {
153 let domainA = document.createElement('a');
154 domainA.href = referer;
155 return domainA.hostname;
156 }
157}
14export function getDomain (url) {
15 return url.host.split('.').slice(-2).join('.')
16}
24function domain(url) {
25 url = url.replace(/.*\/\//, "");
26 url = url.replace(/\/.*/, "");
27 url = url.replace(/:.*/, "");
28 return url;
29}
205export function getGeneralDomainMinusTLD(url) {
206 if (url.hostIsIp) {
207 return 'IP';
208 }
209 // when domainInfo.domain is null, the hostname does not match a known public suffix (e.g. a
210 // local network hostname).
211 if (!url.domainInfo.domain) {
212 return url.domain;
213 }
214 return url.generalDomain.substring(0,
215 url.generalDomain.length - url.domainInfo.publicSuffix.length - 1);
216}
206function demoDomain(sUrl) {
207 for (nDomain = 0; nDomain < demoDomains.length; nDomain++) {
208 if (sUrl.startsWith(demoDomains[nDomain])) return true;
209 }
210 return folksemanticFrames(sUrl);
211}
29export function getDomain(
30 url: string,
31 options: Partial<IOptions> = {},
32): string | null {
33 resetResult(RESULT);
34 return parseImpl(url, FLAG.DOMAIN, suffixLookup, options, RESULT).domain;
35}
6function subDomains(url) {
7 const subDomainsPtrn = /\$\{u:([\w-_.|]+)\}/.exec(url);
8
9 if (!subDomainsPtrn) {
10 return url;
11 }
12
13 const subDomainsList = subDomainsPtrn[1].split('|');
14
15 return url.replace(subDomainsPtrn[0], subDomainsList[(subDomainsCount++) % subDomainsList.length]);
16}

Related snippets