Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to get the URL for my site collection. One way I thought I could do it was using the window.location in javascript and getting it that way.

I know that you can use ECMA script and the client object model to do this but unsure how.

I have tried:

var context = new SP.ClientContext;
alert(context.get_url());

This is returning /

How would I get it to include the full URL? i.e. http://mysharepoint:29292/

share|improve this question

5 Answers 5

up vote 18 down vote accepted

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;
share|improve this answer
1  
In my experience despite that this approach will work in a production environment, if you have a large team and you don't control their development environment @Matthias approach is better because it will work even when a developer is working with localhost. sharepoint.stackexchange.com/questions/24817/… –  rob.alarcon Mar 5 '13 at 1:47
    
+1 for Exact Solution –  Harsh Bhavsar Sep 30 '13 at 9:24

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

share|improve this answer
    
var siteUrl = site.get_url(); throws a SP.Res.propertyHasNotBeenInitialized exeception –  John Dec 7 '11 at 14:28
    
I have updated the sample..., it works now. –  Matthias Dec 7 '11 at 16:25
1  
Just for John - the difference between normal SP object model programming and the Client object model is that nothing actually happens until ctx.executeQueryAsync - so in previous example you were asking for site properties while it was still 'null' (not strictly null but you get the idea) –  Ryan Dec 7 '11 at 17:48
    
I had to add this code to Mattias' answer to get this all working: // Call function InitSiteUrl() after SP resources are loaded ExecuteOrDelayUntilScriptLoaded(InitSiteUrl, 'sp.js'); –  samspot Mar 20 '12 at 17:18
function initialize()
    {
       var clientContext = new SP.ClientContext.get_current();
       siteCollec = clientContext.get_site();
       clientContext.load(siteCollec);
       clientContext.executeQueryAsync(Function.createDelegate(this, getUrl), Function.createDelegate(this, getFailed));
     }


function getUrl()
    {
       alert(site.get_url());
    }
    function getFailed()
    {
       alert('Failed to retrieve the server relative URL.');
    }
share|improve this answer
<script language="javascript">
            _spBodyOnLoadFunctionNames.push("GrabUrl");
            function GrabUrl()
            {
                // Custom JavaScript methods
                var currentUrl = document.URL;
                alert(currentUrl);
</script>
share|improve this answer

You can get the site url like this:

var siteUrl = "<%= SPContext.Current.Web.Url %>";
share|improve this answer
    
Not in pure JavaScript. Your solution is dependent on the ability to run server side code. An ability you do not have in for example the app model or in master pages and page layouts in 2013, not to mention SharePoint Online –  Robert Lindgren 16 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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