Common sources of unterminated string literal

JavascriptDebuggingFirefox Addon

Javascript Problem Overview


I am trying to debug a JavaScript script that gets read in a Firefox extension and executed. I only can see errors via the Firebug console (my code is invisible to Firebug), and it's reporting a "unterminated string literal."

I checked the line and the lines around it and everything seems fine-parentheses, braces, and quotes are balanced, etc. What are other possible causes that I should be looking for?

Javascript Solutions


Solution 1 - Javascript

Most browsers seem to have problems with code like this:

var foo = "</script>";

In Firefox, Opera and IE8 this results in an unterminated string literal error. Can be pretty nasty when serializing html code which includes scripts.

Solution 2 - Javascript

Look for linebreaks! Those are often the cause.

Solution 3 - Javascript

I would vote for jamtoday's answer if I had the "reputation"

If your data is coming by way of PHP, this might help

$str = str_replace(array("\r", "\n"), '', $str);

Solution 4 - Javascript

I just discovered that "<\/script>" appears to work as well as "</scr"+"ipt>".

Solution 5 - Javascript

Just escape your tag closures or use ascii code

ie

<\/script>

ie

<&#47;script>

Solution 6 - Javascript

You might try running the script through JSLint.

Solution 7 - Javascript

Look for a string which contains an unescaped single qoute that may be inserted by some server side code.

Solution 8 - Javascript

If you've done any cut/paste: some online syntax highlighters will mangle single and double quotes, turning them into formatted quote pairs (matched opening and closing pairs). (tho i can't find any examples right now)... So that entails hitting Command-+ a few times and staring at your quote characters

Try a different font? also, different editors and IDEs use different tokenizers and highlight rules, and JS is one of more dynamic languages to parse, so try opening the file in emacs, vim, gedit (with JS plugins)... If you get lucky, one of them will show a long purple string running through the end of file.

Solution 9 - Javascript

Have you escaped your forward slashes( / )? I've had trouble with those before

Solution 10 - Javascript

I've had trouble with angled quotes in the past ( ‘ ) usually from copy and pasting from Word. Replacing them with regular single quotes ( ' ) does the trick.

Solution 11 - Javascript

Maybe it's because you have a line break in your PHP code. If you need line breaks in your alert window message, include it as an escaped syntax at the end of each line in your PHP code. I usually do it the following way:

$message = 'line 1.\\n';
$message .= 'line 2.';

Solution 12 - Javascript

Also, keep in mind that %0A is the linefeed character URL encoded. It took me awhile to find where there was a linefeed in my offending code.

Solution 13 - Javascript

If nothing helps, look for some uni-code characters like

\u2028

this may break your string on more than one line and throw this error

Solution 14 - Javascript

Have you tried Chromebug? It's the Firebug for extensions.

Solution 15 - Javascript

Try a "binary search". Delete half the code and try again. If the error is still there, delete half the remaining code. If the error is not there, put what you deleted back in, and delete half of that. Repeat.

You should be able to narrow it down to a few line fairly quickly. My experience has been that at this point, you will notice some stupid malformed string.

It may be expedient to perform this on a saved version of the HTML output to the browser, if you're not sure which server-side resource the error is in.

Solution 16 - Javascript

The web page developer guessed wrong about which encoding is used by the viewer's browser. This can usually be solved by specifying an encoding in the page's header.

Solution 17 - Javascript

Scan the code that comes before the line# mentioned by error message. Whatever is unterminated has resulted in something downstream, (the blamed line#), to be flagged.

Solution 18 - Javascript

Whitespace is another issue I find, causes this error. Using a function to trim the whitespace may help.

Solution 19 - Javascript

str = str_replace(array("\r\n","\n\r","\r", "\n"), '<br />', stripslashes($str));

This should work.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsuteeView Question on Stackoverflow
Solution 1 - JavascriptVoYView Answer on Stackoverflow
Solution 2 - JavascriptJJ.View Answer on Stackoverflow
Solution 3 - JavascriptPJ BrunetView Answer on Stackoverflow
Solution 4 - JavascriptQueueView Answer on Stackoverflow
Solution 5 - JavascriptBrianView Answer on Stackoverflow
Solution 6 - JavascriptAndrew HedgesView Answer on Stackoverflow
Solution 7 - JavascriptManuView Answer on Stackoverflow
Solution 8 - JavascriptGene TView Answer on Stackoverflow
Solution 9 - JavascriptmeouwView Answer on Stackoverflow
Solution 10 - JavascriptcbaigorriView Answer on Stackoverflow
Solution 11 - JavascriptIpsoratioView Answer on Stackoverflow
Solution 12 - JavascriptTyndareusView Answer on Stackoverflow
Solution 13 - JavascriptPredte4aView Answer on Stackoverflow
Solution 14 - JavascriptÁlvaro GonzálezView Answer on Stackoverflow
Solution 15 - JavascriptChase SeibertView Answer on Stackoverflow
Solution 16 - JavascriptWindows programmerView Answer on Stackoverflow
Solution 17 - JavascriptChris NoeView Answer on Stackoverflow
Solution 18 - JavascriptLeaView Answer on Stackoverflow
Solution 19 - JavascriptVishal VenugopalView Answer on Stackoverflow