Uncaught SyntaxError: Unexpected token < or Uncaught ReferenceError: function is not defined error in JavaScript in SharePoint Online

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 SyntaxError: Unexpected token javascript
Uncaught SyntaxError: Unexpected token javascript

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:

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.

>