In a WebApp with diffrent site collections and subsites, I would like to add a link on the custom master page of all sites to the WebApp Url. (This Url is corresponding with a home/top site collection)

E.g. http://mysp2010farm/sitecollection1/site1/default.aspx is a site and in my custom master page I need a link with href="http://mysp2010farm"
This needs to be done dynamic and not hardcoded.

I thought of some Javascript to do this but got lost on how to achive this (document.getElementById(MyLink).href=window.location.protocol + "//" + window.location.host; )


Is this across multiple web applications, or a single web application? If it's just one web app, all you need is a relative URL to the root site...

<a href="/">Home</a>would take you to http://mysp2010farm regardless of where you are in the web application.

  • It's in one Web App but I would like to use the master page in diffrent Web Apps with the same structure (Dev,Test, Prod) So if I understood correctly when I click on the link <a href="/">Home</a> on the page mysp2010farm/sitecollection1/site1/default.aspx I will be directed to mysp2010farm ? Will try this anyway!
    – Wim Hill
    Aug 31 '11 at 15:58
  • You can do that and reuse it across multiple web applications, it will always take you to the root site for that web application. It's just a relative URL to root, so root of the current web application. If you're on mysp2010farm/managedpath/sitecol it will point to mysp2010farm. If you're on portal.mysp2010farm/managedpath2/sitecol it'll point to portal.mysp2010farm. Always the root of whatever web application the user is at. If you're trying to script it you're making it way harder than it needs to be :-)
    – webdes03
    Aug 31 '11 at 20:37

Well, you could get this via the client object model, but simpler might just be a regular expression

function getServer(url) {
  var regex = new RegExp("https?://[^/]+/","i");
  var match = regex.exec( url );
  if( match != null ) {
    return( match[0] );

Note that this isn't a SharePoint specific solution. Call using something like:

alert( getServer("http://server/Sites/sitecoll/site/lists/list/" ) );
  • Thanks for your answer Andy. I'm looking for any solution to get the following link in my master page: <a href="mysp2010farm">Home/Top Site</a>
    – Wim Hill
    Aug 31 '11 at 15:53
  • I like to create a master page that can be used in diffrent Web Applications (Dev, Test, Prod) but in one solution package. So if you now how I could retrieve/create this link in my master page?
    – Wim Hill
    Aug 31 '11 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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