Shantanu Tomar
Shantanu Tomar

Reputation: 1712

Getting "unexpected token" error in Javascript

I wanted to give a little background first, I was facing the below issue in SSR React using NextJS. In my <head> I have <script></script> tag in which I have multiple <script> tags nested containing minified JS code for Google Analytics, Adobe Analytics, etc. To debug the issue, I tried to break it up more and only including one JS code at a time in my outer <script> tag like only GA or only Adobe to see where it is breaking.

It works fine when I use <noscript> tag and inside it, I have multiple <script> tags containing minified JS for GA, AA, etc. But it breaks my purpose, as when page loads Google Tag Assistant does not catch the tags.

To my surprise, when I tried running below a simple JS code (PSB SS) in incognito mode (just to make sure plain browser profile without any extensions/plugins) I get the same error. Even if I try without incognito mode I get the same error for this simple JS code.

Uncaught SyntaxError: Unexpected token '<'

What is the issue here and why am I getting this error?

enter image description here

Upvotes: 0

Views: 1701

Answers (1)

Swimmer F
Swimmer F

Reputation: 903

Java script belongs inside <script> tags, but <script> tags themselves are HTML, therefore they are not expected, and thats why you get the Unexpected < token.

In other words, don't nest <script> tags. Removing the outermost Tag should fix the error.

Upvotes: 2

Related Questions