views:

5797

answers:

12

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?

+3  A: 

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

Manu
+5  A: 

You might try running the script through JSLint.

Andrew Hedges
I've tried that. The output is too noisy. Too many formatting details, and I didn't see anything that would cause the script not to run.
thaiyoshi
+2  A: 

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.

Chase Seibert
A: 

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.

Windows programmer
+4  A: 

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.

Gene T
A: 

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.

Chris Noe
+4  A: 

Look for linebreaks! Those are often the cause.

jamtoday
A: 

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

meouw
+6  A: 

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.

VoY
This was the cause of my error, even though the script passed JSLint. I changed "</script>" to "</scr"+"ipt>" and that fixed it.
richardkmiller
Ugh. Is there a reason behind this?
Jere
A: 

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

Lea
A: 

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

Álvaro G. Vicario
A: 

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.

cbaigorri