Getting "unexpected token" error in Javascript - 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?

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.

Related

Put JavaScript code into a string variable

I have some data wanted to be represented using datable with jQuery. As you can see, it is easy to use by including totally two, css and javascript, files. Since, as my project requirement, neither I include any file in project folder or somewhere nor I’m able to connect to the Internet, I need to escape all javascript code into a string variable then use it between <script></script> tags. To do so, I make use of the website. I think it works pretty well since its result doesn’t yield any compiler error or warning. Whenever I include it as,
<script type=\"text/javascript\" language=\"javascript\" src=\"https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js\"></script>
it works. But when I put the content of the jquery.dataTables.min.js into a string variable by escaping via the website, I get the following error.
44th line’s the following part is hightlighted with the cross end symbol on chrome. (I’m not able to put whole code here becuase SO doesn’t permit it.)
.warn("jQuery.Deferred exception: "+e.message,e.stack,t)},E.readyException=function(e){g.setTimeout(function(){throw e})};var $=E.Deferred();function F(){v.removeEventListener("DOMContentLoaded",F),g.removeEventListener("load",F),E.ready()}E.fn.ready=function(e){return $.then(e)["catch"](function(e){E.readyException(e)}),this},E.extend({isReady:!1,readyWait:1,ready:function(e){(!0===e?--E.readyWait:E.isReady)||(E.isReady=!0)!==e&&0<--E.readyWait||$.resolveWith(v,[E])}}),E.ready.then=$.then,"complete"===v.readyState||"loading"!==v.readyState&&!v.documentElement.doScroll?g.setTimeout(E.ready):(v.addEventListener("DOMContentLoaded",F),g.addEventListener("load",F));var z=function(e,t,n,r,i,o,a){var s=0,u=e.length,l=null==n;if("object"===T(n))for(s in i=!0,n)z(e,t,s,n[s],!0,o,a);else if(void 0!==r&&(i=!0,x(r)||(a=!0),l&&(a?(t.call(e,r),t=null):(l=t,t=function(e,t,n){return l.call(E(e),n)})),t))for(;s<u;s++)t(e[s],n,a?r:r.call(e[s],s,t(e[s],n)));return i?e:l?t.call(e):u?t(e[0],n):o},_=/^-ms-/,U=/-([a-z])/g;function V(e,t){return t.toUpperCase()}function X(e){return e.replace(_,"ms-").replace(U,V)}var Q=function(e){return 1===e.nodeType||9===e.nodeType||!+e.nodeType};function Y(){this.expando=E.expando+Y.uid++}Y.uid=1,…………….. STACKOVERFLOW DOESN’T PERMIT LONG TEXT…………………..function(e,t,n){var r,i;if(e&&e.preventDefault&&e.handleObj)return r=e.handleObj,E(e.delegateTarget).off(r.namespace?r.origType+"."+r.namespace:r.origType,r.selector,r.handler),this;if("object"==typeof e){for(i in e)this.off(i,t,e[i]);return this}return!1!==t&&"function"!=typeof t||(n=t,t=void 0),!1===n&&{return g.$===E&&(g.$=xt),e&&g.jQuery===E&&(g.jQuery=bt),E},e||(g.jQuery=g.$=E),E});
What’s wrong with my idea? Cannot I apply my idea?

(index):889 Uncaught SyntaxError: Unexpected token <

I'm using Google Analyticator and my website is http://gestionalemagazzinoonline.it/
You can see the error in the javascript console.
I think that is the suspected file:
https://github.com/wp-plugins/google-analyticator/blob/master/google-analyticator.php
The problem is also if i disable the plugin, or if i change the source code i don't see the changes (maybe there's a cache?).
How can I fix?
Script tags are meant to write javascript in them, and not html.
You have a script block inside a script block. Remove that, and it will work all fine.

Chrome Sources Displaying Javascript in Chinese

My javascript was working properly and then all of the sudden it wasn't. Chrome give me the Uncaught SyntaxError: Unexpected token ILLEGAL error on line 1 of my JS file called para2.js. I removed script from the file except for the following and I still get the error:
$( window ).ready(function() {
});
My jQuery file is included in the head of my document:
<script src="../shared/jquery.js" type="text/javascript"></script>
<script src="js/para2.js" type="text/javascript"></script>
When I look at my js file in Chrome developer tools on the Sources tab, it displays the script in Chinese:
兪敵祲搨捯浵湥⥴爮慥祤昨湵瑣潩⡮笩⥽
When I view the source of the js file, it looks fine. I have read similar posts that say copy and pasting from JSFiddle can include some hidden characters that may cause it, but I have started from scratch writing this file from a blank text file and copy and pasting nothing. It is strange that it was working and now it is not. Did my server get hacked or something? And help is appreciated.
You should use the charset attribute to indicate which charset you are using.
<script src="../shared/jquery.js" type="text/javascript" charset="UTF-8">
I had the same problem with jquery-1.5.1.min.js. Chrome kept evaluating it as Chinese (scripts worked fine in IE). I finally solved the problem by re-encoding the .js files using "convert to UTF-8" in Notepad++. Note, not UTF-8 without BOM, but UTF-8.

Spontaneous Unterminated String Literal Error when trying to include external OpenLayers Javascript file in html

I get an odd unterminated string literal error when opening my website on my Laptop at home. If I try to open the website at work (where I programed it), the error does not appear. First I started to compare the Firefox versions, but both are the same -> 19.0.
After trying some "Unterminated String Literal"-Error Fixes, like they are described in many threads here on the stackoverflow site, I got more errors in the external js file. So I thought I have an error in the inclusion of this file and tried different possibilities of inclusions (f.e. with and without adding type="javascript" in the script tag). After a few vain tries, I decided to put it back to its previous state:
<script src="js/openLayers/OpenLayers.js"></script>
The error that is still appearing says
"SyntaxError: unterminated string literal" in string
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAQAIBRAA7"
in OpenLayers.js on line 48.
So I tried to put the whole OpenLayers.js in my html as internal Javascript and everything worked fine.
Has anyone an idea why this error is only occuring when the OpenLayers.js is included as external js?

Why the copied HTML doesn't look like the original HTML?

I copied the generated source code (View Source -> View Generated Source in the Firefox Web Developer Toolbar) of Google's Keyword Tool page to a new HTML file.
But, when I open this new file, some of the items looks stretched for some reason:
The original website looks like this:
I guess that Google create some elements and set various attributes using Javascript, but I copied the page after it has been generated. So, why is this difference?
UPDATE 1
The only JS/CSS file, which is not given as a full path, is:
<script language="javascript" src="/cues/cues.js">
I tried replace this with:
<script language="javascript">
Contents of '/cues/cues.js' here
</script>
but it didn't help.
UPDATE 2
In the browser's error console I found the following 2 errors:
Error: com_google_ads_apps_servers_cues_CuesRelease is not defined
Source File: https://adwords.google.com/cues/768DAEDDB2193AB5B05B9C6A01394D78.cache.js
Line: 1
Error: com_google_ads_apps_targetingideas_client_TargetingIdeas is not defined
Source File: https://adwords.google.com/o/Targeting/756D6AF3BB4DD4A68315E34F50C2BC7E.cache.js
Line: 1
Any ideas why these errors appear?
UPDATE 3
Apparently, the reason is that the DOCTYPE declaration is missing. After I added <!DOCTYPE html> to the stretched version, it solved the problem. Can anyone explain why?
When you save a page, you only get the version of HTML served from the server in its original form. Any mods to the DOM made after load using JS will not be part of the save.
EDIT
I could not trace out the exact reason for the error as the code is really cryptic! In any case, if all you want is to be able to reproduce the exact page offline, then you can do a 'save page as..' from your browser (choose web page, complete). I tried this with FF as well as Chrome and it is working fine in both cases. While opening the saved page, it might be best not to use IE as its a certified choker when it comes to even the slightest error in code. :)
The most likely reason for the error is an cross-domain AJAX security exception (fired when the calling client side script and called server side script are from different domains). The 2 variables namely, com_google_ads_apps_servers_cues_CuesRelease and com_google_ads_apps_targetingideas_client_TargetingIdeas seems to be initialized using the return of some AJAX call (which couldn't execute bcoz of the secu excep), and as a result remain as undefined.
You must be missing some css and js which is not on the page but referred from somewhere else.
The most probable reason is that the CSS and the corresponding images that might be referred within it are not getting applied correctly.
Check the paths of the CSS and for the images (background) within the CSS...You might need to correct the paths to fix the issue.

Categories