Sharepoint - Get Site Collection full URL using javascript
You can do it without any SharePoint calls by using the default JavaScript location object (W3schools) and the page variable _spPageContextInfo
(Ted Pattison's Blog).
Something like:
var url = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;
You can do it like this:
<script>
function GetSiteUrl()
{
var ctx = new SP.ClientContext();
var site = ctx.get_site();
ctx.load(site);
ctx.executeQueryAsync(function(s, a){alert(site.get_url())});
}
</script>
<a href='javascript:GetSiteUrl();'>Get site URL</a>
To load only the URL from the site to minimize data traffic you can also call:
ctx.load(site, 'Url');
For more see reference: http://msdn.microsoft.com/en-us/library/ee538253.aspx
You can do it in Singleline of code
var siteCollectionURL = _spPageContextInfo.siteAbsoluteUrl;