I Include an external javascript file in an Html file, when I browse the Html file in a web browser, I meet the javascript error with the error message Uncaught SyntaxError: Invalid or unexpected token. This article will tell you how to fix it.
1. How To Reproduce The JavaScript Uncaught SyntaxError: Invalid Or Unexpected Token.
- The Html file name is test.html.
- In the test.html file, it imports an external javascript file test.js using the below code.
<script type="text/javascript" src="test.js" ></script>
- Open the Html file test.html in the Google Chrome web browser.
- Right-click the web page in the web browser, then click the Inspect menu item in the popup menu list to open the web browser Inspector window.
- Click the Console tab on the web browser’s Inspector window.
- Then you can see the error message Uncaught SyntaxError: Invalid or unexpected token in the console output.
2. How To Fix The JavaScript Uncaught SyntaxError: Invalid Or Unexpected Token.
- The main reason for such error is because there are some unnormal characters in the javascript code that are not encoded in the correct charset.
- You just need to remove the javascript code in the error line, and to see whether the error disappears.
- Do not forget to refresh the test.html and the test.js file to make the change take effect.
- You can also copy the javascript code line that has the error occurs to the text editor such as Sublime Text.
- Then you can find the unnormal character in it and remove it to fix the error.
<0x200b>// Create a SnowFlakeApp instance when the window is loaded. window.onload = new SnowFlakeApp();
- In the above example javascript code, the character <0x200b> is unnormal, remove it then you can fix the error.