var baseUrl = getBaseUrlforSharepointService(); function getBaseUrlforSharepointService() { try { var absUrl = window.location.href; var layoutsCharPosition = window.location.href.indexOf("SitePages") <= 0 ? ( window.location.href.indexOf("SitePages") <= 0 ? ( window.location.href.indexOf("SitePages") <= 0 ? window.location.href.indexOf("SitePages") : window.location.href.indexOf("SitePages") ) : window.location.href.indexOf("SitePages") ) : window.location.href.indexOf("SitePages"); var result = ''; //check for the string "_layouts". Returns -1 if not found,else // returns the char position if (absUrl.indexOf("SitePages") >= 0 || absUrl.indexOf("SitePages") >= 0 || absUrl.indexOf("SitePages") >= 0 || absUrl.indexOf("SitePages") >= 0) { result = absUrl.substr(0, layoutsCharPosition); } if (result.indexOf("localhost") === 7) { return "http://sharepoint.server.com/site"; } return result; } catch (error) { alert("Could not Find Site Url") } }
Keep coding!