javascript:unterminated string literal workaround

Source: Internet
Author: User

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>&nbsp;&nbsp;");
s = S.replace ("\ r \ n", "<br>&nbsp;&nbsp;"); /This is the right one!
s= s.replace ("\ T", "&nbsp;&nbsp;&nbsp;&nbsp;");
S =s.replace ("", "&nbsp;");

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>&nbsp;&nbsp;");
s = S.replace ("\ r \ n", "<br>&nbsp;&nbsp;"); /This is the right one!
s= s.replace ("\ T", "&nbsp;&nbsp;&nbsp;&nbsp;");
S =s.replace ("", "&nbsp;");

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.