" displays an unterminated string literal js error string I am extracting some data from Db and displaying it in a text ...">

A string having "<br/">" displays an unterminated string literal js error string

I am extracting some data from Db and displaying it in a text box using jquery as follows. $('#textareatest').val('<% =teststring %>'). It is possible that the string teststring may contain line breaks XHTML ( <br/>). whenever the line contains <br/>, I get an error 'unterminated string literal'. I saw several posts viewing '\ n' as line breaks and suggesting avoiding this. I tried to avoid the <br/>same way, but that didn't work.

Can someone help me with this?

UPDATE: . I have already escaped single quotes. here is an example string test string<br />

Thank.

+3
source share
4

, teststring ("\n"), ​​, JavaScript ,

$("#textareatest").val('Some random text
then an unexpected linebreak');

, teststring .

, teststring - , :

$("#textareatest").val('Some random test but that single quote kills the string');
+2

, - . , , .

, , , - , , - .

, , , , , .

, - -, , , . , :

, ( ") (') . ; . :

" blah "'blah'" 1234 "" \n " , , . , JavaScript. \b backspace\f form feed\n \r \ t tab\

4,5.

, , , .

, . . ,

var quote =" "The Cremation " R.W. Service. "document.write()

" " R.W. Service. , . , c:\temp , :

var home =" c:\temp"

:

  • - ,

  • console.log , .

, - , - , .

0

, :

$('#textareatest').val(decodeURI('<% =System.Uri.EscapeUriString(teststring) %>'))

?

This will encode the entire string for use in URLs and allow javascript to decode it. It is not very efficient, but it is pretty safe (since the character set allowed in the URI is pretty small).

0
source

You must encode the html string, so it <br/>will become:

&lt;br/&gt;
-1
source

Source: https://habr.com/ru/post/1788520/


All Articles