[Solved] Uncaught ReferenceError: web is not defined Error in SharePoint Online JSOM

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

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

Uncaught ReferenceError: web is not defined
Uncaught ReferenceError: web 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 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>

You may like following jsom SharePoint tutorials:

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

>