Get domain name url and url parts in javascript

 With JavaSctipt we can extract to get protocal, host, pathname and search path... Now we will research how to get domain name, url and url parts with JavaSctipt

Get domain name url and url parts in javascript


  • window.location.protocol = “http:”
  • window.location.host = “localhost:44387
  • window.location.pathname = “/expath/index.html”
  • window.location.search = “?s=condition”

1. Get Domain Name (Current URL at localhost https://localhost:44387/)

 var rootDomain = window.location.hostname.split('.').slice(-2).join('.');

  alert(rootDomain);



Result: localhost

2. Get URL with protocol and port
var rootDomain = window.location.protocol + "//" + window.location.host + "/" 



Result: https://localhost:44387

Reactions

Post a Comment

0 Comments