Skip to main content

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;