How to dynamically construct the SharePoint site relative url in JavaScript

By namwar - Last updated: Friday, January 1, 2010 - Save & Share - 3 Comments

There are times when you want to dynamically construct a SharePoint site relative Urls e.g. if you are calling a SharePoint webservice from JQuery etc. then you will need to call it in context of your required site.

Most people try to slice and dice the URL of the current page and try to find the SharePoint site from it. This is not a very clean way of doing it.

The more easy and clean way is the way SharePoint itself does which is using the JavaScript variable called L_Menu_BaseUrl

This variable holds the relative url of current site.

So for example, if you want to call Lists.asmx for

http://yourintranet/sites/Finance/ then

L_Menu_BaseUrl + ‘/_vti_bin/Lists.asmx’ will give you the required url for web service.

Posted in JavaScript • Tags: , , Top Of Page

3 Responses to “How to dynamically construct the SharePoint site relative url in JavaScript”

Comment from Search engine Optimization
Time August 15, 2010 at 5:46 am

Hi thr, thanxc a lot for this blog …….. This is what I was looking for.

Comment from sharepoint csharp
Time September 23, 2010 at 12:26 pm

This is useful – does it apply in code behind? I can’t seem to find the relative url for use within a menu tag :/

Comment from Bassam Tantawi
Time December 3, 2012 at 12:48 pm

Thank you very much, useful one :)

Write a comment