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
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:
- How to create a
list using jsom (JavaScript object model) in SharePoint? - Top 51 JSOM SharePoint Examples (JavaScript object model) in SharePoint Online, SharePoint 2019/2016/2013 (Download 100 Pages PDF EBook FREE)
- jsom SharePoint 2013 example: Bind SharePoint list items to dropdownlist using javascript object model (jsom) in SharePoint Online
- Get list item by id using jsom (JavaScript object model) in SharePoint Online/2016/2013
- Retrieve SharePoint list items programmatically using jsom, rest API and csom in SharePoint Online/2016/2013
- Retrieve all users from all groups using jsom in SharePoint Online
- Rename SharePoint List Title Column Programmatically using PnP CSOM
I hope this jsom SharePoint tutorial helps to resolve Uncaught ReferenceError: web is not defined issue in SharePoint Online Office 365.
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com