This JSOM tutorial, we will discuss how to fix error Uncaught SyntaxError: Unexpected token < or Uncaught ReferenceError: function is not defined which comes in JavaScript in SharePoint Online.
Recently we were working on JavaScript Object Model (jsom) in SharePoint Online. For that, we have created an HTML file that contents our Html code and then we created one js file which contains the js code.
And we have referred the js file in the HTML page. But when we run the script it gave error as Uncaught SyntaxError: Unexpected token < and Uncaught ReferenceError: showAlert is not defined. Here showAlert is our javascript function name.
The error looks like below:

Uncaught ReferenceError: function is not defined
We had a simple HTML form which has only an input button control like below:
HTML File:
<html>
<head>
<script src="https://onlysharepoint2013.sharepoint.com/sites/Bhawana/SiteAssets/demo.js"></script
</head>
<body>
<h1>My First Heading</h1>
<p></p>
<input type='button' value='Get Alert' onclick="showAlert();" />
</body>
</html>
And Our Js File Looks like below:
<script>
function showAlert() {
alert('hello');
}
</script>
These two filed we have added in the Site Assets document library and we have added the HTML file into a web part page through a content editor web part. When we click on the button, nothing happened and the above error comes.
If you will check the method name is the same and defined. But still, the error was coming.
The issue was coming because of the script tag we added in the .js file.
We do not need to add the <script> </script> tag.
So the .js file will contains only the function which looks like below:
function showAlert() {
alert('hello');
}
Read some SharePoint jsom tutorials:
- Display List items in HTML Table using JSOM in SharePoint Hosted App
- Uncaught ReferenceError: web is not defined Error in SharePoint Online JSOM
- How to get SharePoint person or group field using javascript (jsom)
- Retrieve SharePoint list items programmatically using jsom, rest api and csom
- How to create a list using jsom (JavaScript object model) in SharePoint?
- Retrieve all users from all groups using jsom in SharePoint
- Display SharePoint List Items in Div using JSOM (javascript object model)
- Retrieve SharePoint List Items and display in Hyperlink using JavaScript Object Model (jsom)
- Bind SharePoint list items to dropdownlist using javascript object model (jsom), Rest API and Server Object Model
- Uncaught ReferenceError $ is not defined
This SharePoint tutorial, we learned how to fix error, Uncaught SyntaxError: Unexpected token < or Uncaught ReferenceError: function is not defined error in JavaScript in SharePoint Online.
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