_spBodyOnLoadFunctionNames.push("FetchURL");
function FetchURL()
{
// Custom JavaScript methods
var ctx = new SP.ClientContext();
var site = ctx.get_site();
ctx.load(site);
ctx.executeQueryAsync(function(s, a){ currURL=SP.Utilities.Utility.getLayoutsPageUrl("");
});
}
The above method returns the URL, however, sometimes it returns undefined, which causes errors. How to tackle this?
Code is placed in head of the masterpage... in scripts tag.
undefined? The function FeetchURL has no return and will then always returnunderfined. The global variablecurrURLwill be set asynchronously, asctx.executeQueryAsyncis asynchronous, which means in you code you can't know when it is set – eirikb Apr 04 '14 at 10:18_spPageContextInfo.layoutsUrlinstead. http://blah.winsmarts.com/2013-2-_sppagecontextinfo_is_your_new_best_friend.aspx – Derek Gusoff Apr 04 '14 at 10:47