SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;

This is returning /

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

share|improve this question
up vote 38 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 + "//" + + _spPageContextInfo.siteServerRelativeUrl;
share|improve this answer
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.… – rob.alarcon Mar 5 '13 at 1:47
+1 for Exact Solution – Harsh Bhavsar Sep 30 '13 at 9:24
Great solution. Although I also needed the path to the current web which your solution didn't really take it all the way. But _spPageContextInfo.webServerRelativeUrl held what I was looking for. – Mohag519 Jan 20 '15 at 14:06
This wont work in 2007 will it? – Batman Jun 5 at 16:21

You can do it like this:

function GetSiteUrl()
    var ctx = new SP.ClientContext();
    var site = ctx.get_site();
    ctx.executeQueryAsync(function(s, a){alert(site.get_url())});
<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:

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
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

You can do it in Singleline of code

var siteCollectionURL = _spPageContextInfo.siteAbsoluteUrl;
share|improve this answer
This is correct for SharePoint 2013, SP2010 _spPageContextInfo does not have the property siteAbsoluteUrl. – Anders Aune Feb 20 '15 at 10:54
function initialize()
       var clientContext = new SP.ClientContext.get_current();
       siteCollec = clientContext.get_site();
       clientContext.executeQueryAsync(Function.createDelegate(this, getUrl), Function.createDelegate(this, getFailed));

function getUrl()
    function getFailed()
       alert('Failed to retrieve the server relative URL.');
share|improve this answer

For this you can use any of the two:

var url = window.location.protocol + "//" + + _spPageContextInfo.siteServerRelativeUrl;

Or we can use

var context = new SP.ClientContext();
var relativeWebUrl = context.get_url();
var fullWebUrl = window.location.protocol + '//' + + relativeWebUrl ;
share|improve this answer

You can use the following:

For Site Collection URL:

var url = window.location.protocol + “//” + + _spPageContextInfo.siteServerRelativeUrl;

For Sub Site URL:

var url = _spPageContextInfo.webAbsoluteUrl;
share|improve this answer

You can use the below code to find WebApp url, this takes care of port number too (if exists)

var WebAppUrl="";
if (location.port)
        WebAppUrl = window.location.protocol + "//" + + ":" + location.port;
        WebAppUrl = window.location.protocol + "//" +;
share|improve this answer
<script language="javascript">
            function GrabUrl()
                // Custom JavaScript methods
                var currentUrl = document.URL;
share|improve this answer

Your Answer


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.