How to get site collection url using javascript? [

2019-08-04 20:10发布

问题:

I am stuck up in a requirement where i need to get the site collection url to pass in a function. Please help?

回答1:

You can use the L_Menu_BaseUrl variable, it is set automatically by some of SharePoint's default scripts.

You can find more information about it here

  • http://vrdmn.blogspot.com/2011/08/javascript-lmenubaseurl-varaible-for.html
  • http://weblogs.asp.net/jan/archive/2009/05/18/determine-the-url-of-sharepoint-web-services-using-jquery-javascript.aspx


回答2:

you have 2 options: 1) in your javascript use the L_Menu_BaseUrl variable. it contains the relative url of the site collection 2) if you need the absolute url you can use the following javascript code snippet in the server control:

<script type="text/javascript">
    var siteUrl = "<%= SPContext.Current.Site.Url %>";
</script>

It renders absolute url to the site collection. the approach (2) allows you to form up the url you need on the server side.

I hope you got the idea. Good luck.