1

I need to get the the site colllection url using the javascript. I have written one simple function as below

function getSiteCollectionUrl()
{
var pageUrl= window.location.href;
var protocol = pageUrl.split(":")[0];
var addr=pageUrl.split("//")[1];
var webUrl = addr.split("/")[0];
var siteColleUrl = protocol + "://" + webUrl 
}

let say the site address is "http://mysite/trialsite/default.aspx", then it will return: "http://mysite" But I think this isn't proper way to get the site collection url.

PLease suggest if you have any other idea.

2 Answers 2

2

Just by looking at SharePoint tag, right below this question, there is exactly the same question: How to get site collection url using javascript?

And the answers are there. And luckily you can get SharePoint site collection URL without invoking SharePoint API, because there is a L_Menu_BaseUrl variable available that contains it.

At http://server/documents it returns:

console.log(L_Menu_BaseUrl)
/documents 
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot obtain the Site collection Url from just pure javascript without invoking SharePoint API.

If you need to do it in Javascript, you can invoke SharePoint API through web services and get the Site collection Url.

There is already Javascript API available called SPServices. You can use the following function:

SPGetCurrentSite

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.