I was working on a project where I needed to get the current web's ID or URL (sometype of unique identifier) in javascript without making an service call (executeQueryAsync). I didn't seem to be able to find a method that would return this information before a load and executeQueryAsync. Through IE dev tools I was able to find the value of the url and the ID in the object but it was stored in some strang variables ($0_0.$1P_0 and $0_0.$r_0[0].$M_1). Does anyone know of a supported way to get this information before the web object is loaded through the executeQueryAsync. Here's some code to refer to, I basically don't want to perform the extra service call for this website if I've already done it once in this session for this website.

function DoStuff() {
    if ( GetCookie("MyStuff"+SP.ClientContext.get_current().get_web().$0_0.$1P_0) ) return;

    var listTitle = "Stuff";

    var context = new SP.ClientContext.get_current();
    this.web = context.get_web();
    this.user = web.get_currentUser();
    this.list = web.get_lists().getByTitle(listTitle);

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Author' /><Value Type='Integer'><UserID /></Value></Eq></Where></Query><RowLimit>1</RowLimit></View>");
    this.listItems = list.getItems(camlQuery);


    context.executeQueryAsync(StuffSucceeded, StuffFailed);

function StuffSucceeded(sender, args) {
    SetCookie("MyStuff" + SP.ClientContext.get_current().get_web().$0_0.$1P_0, 'True', '/');

function StuffFailed(sender, args) {} 

ExecuteOrDelayUntilScriptLoaded(DoStuff, "sp.js");

2 Answers 2


You can get the server-relative web url from anywhere in SharePoint by accessing a variable called L_Menu_BaseUrl. If you view source on ant page in SharePoint you'll find it defined.

  • Thanks, I unfortunately never knew about that. I have now found this page which has a few more listed: ruudheemskerk.net/archive/2010/08/03/… Apr 26, 2012 at 16:03
  • L_Menu_BaseUrl is also available for SharePoint 2013. That's great and thousands of thanks to Derek Gusoff!
    – FrankH
    Jul 2, 2013 at 9:48

For SharePoint 2010 I'd recommend using _spPageContextInfo.webServerRelativeUrl

  • Sometimes _spPageContextInfo is null. I found L_Menu_BaseUrl to be safer or even var context = new SP.ClientContext(); var relativeWebUrl = context.get_url();
    – Dennis G
    May 12, 2014 at 15:33

Your Answer

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

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