0

Below is my URL and I want only subdomain i.e dnyaneshtechno

Url : https://dnyaneshtechno.sharepoint.com

As per above URL I want to fetch only 'dnyaneshtechno' I am using angular 8 for front end so Can someone please help me on this.

4 Answers4

7

You can use split and substr functions or get subdomain from an url

Example :

Dynamic link :

let getLink =  window.location.href ; 
console.log(getLink)          // dynamic link 
var  subdomain = getLink.substr(COUNT, 1000).split(".")[0]

Using Split Method :

var Link = "https://SUBDOMAIN.DOMAIN.com"
var subdomain = Link.substr(COUNT, 1000).split(".")[0]
console.log(subdomain)      //SUBDOMAIN

Or Using get subdomain form link

const { hostname }  = new URL('https://SUBDOMAIN.DOMAIN.com')
const [subdomain] = hostname.split('.')
console.log(subdomain)    //SUBDOMAIN

i hope i will be helpful fo all !

Dako patel
  • 760
  • 4
  • 13
2

Assuming you want to extract the subdomain from the string, this isn't an Angular problem, but pure JavaScript. There's the URL constructor that can do this:

const {
  hostname
} = new URL('https://dnyaneshtechno.sharepoint.com')

const [subdomain] = hostname.split('.')

console.log(subdomain)
Adam Azad
  • 11,171
  • 5
  • 29
  • 70
0

You can use substr and split functions, ie.

var a = "https://dnyaneshtechno.sharepoint.com"
var subdomain = a.substr(9, 1000).split(".")[0]
Nebojsa Susic
  • 1,220
  • 1
  • 9
  • 12
0

For an angular based solution by injecting the DOCUMENT from @angular/platform-browser like this

import { Injectable, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';

@Injectable()
export class SampleService {

    constructor(@Inject(DOCUMENT) private document: Document) {
    }

    getDomainname() : string{
        return this.document.location.host.split('.')[0];
    }
}

getDomainName() will return the domain of current page.

Noman Fareed
  • 274
  • 3
  • 11