Uncaught ReferenceError: web is not defined

In this jsom SharePoint tutorial, we will discuss how to solve the Uncaught ReferenceError: web is not defined error, which comes while working with jsom in SharePoint Online.

I was trying to retrieve web site by using jsom in SharePoint Online by using a script editor web part. But when I ran the code, it gave an error as: Uncaught ReferenceError: web is not defined. The error looks like the following:

executeordelayuntilscriptloaded is not defined

Uncaught ReferenceError: web is not defined

Here I was using the below code to retrieve the SharePoint web title using jsom.

<script language="javascript" type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(myMethod,'sp.js');

function myMethod()
{
var context=new SP.ClientContext.get_current();
var web=context.get_web();
context.load(web);
context.executeQueryAsync(success,failure);
}

function success()
{
alert(web.get_title());
}

function failure()
{
alert('Failed');
}

</script>

The error was coming when I was trying to access the web.get_title() in the success() method because the web was declared outside of the success(), in the myMethod() method, which was not accessible.

So I declared the web variable globally, and it started working.

<script language="javascript" type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(myMethod,'sp.js');
var web;
function myMethod()
{
var context=new SP.ClientContext.get_current();
web=context.get_web();
context.load(web);
context.executeQueryAsync(success,failure);
}

function success()
{
alert(web.get_title());
}

function failure()
{
alert('Failed');
}

</script>

I hope this jsom SharePoint tutorial helps to resolve the Uncaught ReferenceError: web is not defined issue in SharePoint Online.

You may also like:

>