Skip to main content
Updated the sample code
Source Link
Matthias
  • 2.1k
  • 13
  • 22

Try to use the get_Current() functionYou can do it like this:

<script>
function GetSiteUrl()
  {
    var ctx = new SP.ClientContext.get_current();
    var site = ctx.get_site();
var siteUrl =  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

Try to use the get_Current() function:

var ctx = SP.ClientContext.get_current();
var site = ctx.get_site();
var siteUrl = site.get_url();

For more see reference: http://msdn.microsoft.com/en-us/library/ee538253.aspx

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

Source Link
Matthias
  • 2.1k
  • 13
  • 22

Try to use the get_Current() function:

var ctx = SP.ClientContext.get_current();
var site = ctx.get_site();
var siteUrl = site.get_url();

For more see reference: http://msdn.microsoft.com/en-us/library/ee538253.aspx