I'm trying to figure out how to get a portion of the url in a Master Page in SharePoint 2016 on-prem. For example, I have several site collections that look like this:

http://mainsite.domain.com/sites/test1/... http://mainsite.domain.com/sites/test2/...

In a master page, how do I trim off everything after the .com to end up with a url that looks like this:


1 Answer 1


Actually, I don't know why you need to get only the hostname in your master page as well as you don't specify where exactly you will use it.

But as per your request, if you need to just get the hostname, try to use the below JS code

var hostname= window.location.hostname;

I suggest to try it first in the console (F12) to make sure that exactly what you need!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.