When JavaScript uses document.write (str) for output, the following errors are often prompted:
Error: unterminated string literal. The usual reason is that the output character Str contains newline characters.
Here's how to fix it:
asp:
Str=replace (Str,vbcrlf, "", 1,-1,1)
php:
$str = Str_replace ("\ n", "", $str);
$str = Str_replace ("\ R", "", $str);
The document.write (str) output is then performed.
Note:
the corresponding function of the JSP:
public static final string htmltocode (string s)
{
if (s = = null)
{
Return "";
} else
{s = s.replace ("\n\r", "<br> ");
s = S.replace ("\ r \ n", "<br> "); /This is the right one!
s= s.replace ("\ T", " ");
S =s.replace ("", " ");
S=s.replace ("\" "," \ \ "+" \ ");//If the original text contains double quotation marks, this sentence is the most critical!!!!!!
return s;
}
}
If you write an error like this
s = S.replace ("\n\r", "<br> ");
s = S.replace ("\ r \ n", "<br> "); /This is the right one!
s= s.replace ("\ T", " ");
S =s.replace ("", " ");
s=s.replace ("\" "," \ \ "+" \ ""); If the original text contains double quotes, this sentence is the most critical!!!!!!
without the last sentence will not be able to judge the double quotation mark error, note that the last sentence is "replace", but pay attention to escape character wording!!!
This is a common solution, and another way to keep the carriage return
Replace \ r \ n with <br> First, and then replace \ r with \ n instead of simply discarding as '.
function my_nl2br ($s)
{
return Str_replace ("\ n", ' <br> ', Str_replace ("\ R", ' <br> ', Str_replace ("\ r \ n", ' <br> ', $s)));
}
javascript:unterminated string literal workaround