[javascript] Common sources of unterminated string literal

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?

This question is related to javascript debugging firefox-addon

The answer is


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.


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.


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.


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


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


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


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.';

You might try running the script through JSLint.


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.


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


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


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


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

This should work.


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.


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.


You might try running the script through JSLint.


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.


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


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.


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.


Look for linebreaks! Those are often the cause.


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

This should work.


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);

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.


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


You might try running the script through JSLint.


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


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


Look for linebreaks! Those are often the cause.


You might try running the script through JSLint.


Look for linebreaks! Those are often the cause.


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);

Just escape your tag closures or use ascii code

ie

<\/script>

ie

<&#47;script>

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


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.


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.


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.


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.


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


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.';

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.


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


Look for linebreaks! Those are often the cause.


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.


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


Just escape your tag closures or use ascii code

ie

<\/script>

ie

<&#47;script>

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.


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.


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.


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


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


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.


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to debugging

How do I enable logging for Spring Security? How to run or debug php on Visual Studio Code (VSCode) How do you debug React Native? How do I debug "Error: spawn ENOENT" on node.js? How can I inspect the file system of a failed `docker build`? Swift: print() vs println() vs NSLog() JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..." How to debug Spring Boot application with Eclipse? Unfortunately MyApp has stopped. How can I solve this? 500 internal server error, how to debug

Examples related to firefox-addon

Drag and drop menuitems Postman addon's like in firefox Convert URL to File or Blob for FileReader.readAsDataURL Getting "net::ERR_BLOCKED_BY_CLIENT" error on some AJAX calls How can I fix WebStorm warning "Unresolved function or method" for "require" (Firefox Add-on SDK) How to add java plugin for Firefox on Linux? Firefox Add-on RESTclient - How to input POST parameters? .setAttribute("disabled", false); changes editable attribute to false What is a MIME type? Parsing JSON from XmlHttpRequest.responseJSON