kranthi
kranthi

Reputation: 1539

string having '<br/>' throws unterminated string literal js error

I am fetching some data from Db and displaying it in a textarea using jquery in the following way. $('#textareatest').val('<% =teststring %>').It is possible that the string 'teststring' can contain XHTML line breaks(<br/>).whenever the string contains <br/> I am getting the 'unterminated string literal' error.I saw a number of posts considering '\n' as line breaks and suggesting to escape it.I tried to escape the <br/> similarly,but it didn't work.

Could someone please help me with this?

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

Thanks.

Upvotes: 3

Views: 5669

Answers (4)

Martijn
Martijn

Reputation: 13622

It might be a bit of overkill, but just to test: does

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

work?

This will encode the entire string for use in URLs, and let javascript decode it. It's not very efficient, but it is rather safe (since the set of characters allowed in a URI is pretty small).

Upvotes: 0

Charlie Martin
Charlie Martin

Reputation: 112366

It would help a lot to see an actual example, but it's going to be something silly and annoying. My guess would be that you're mixing single and double-quotes in your code in a way that ends up with an unmatched outer set as you construct the string.

Update

Well, if that's the exact string you're sending, then it's no problem -- there's nothing to the string literal that's a problem, so you must be mistaken -- there's no error.

Assuming the code persist in not working even after I've told you that, then you probably need to look more deeply.

Look, the error message is pretty clear -- you have something that starts out syntactically as a string literal, but where the interpreter can't find the end. There's really only one syntax for string literals, to quote:

A string literal is zero or more characters enclosed in double (") or single (') quotation marks. A string must be delimited by quotation marks of the same type; that is, either both single quotation marks or double quotation marks. The following are examples of string literals:

"blah" 'blah' "1234" "one line \n another line" In addition to ordinary characters, you can also include special characters in strings, as shown in the last element in the preceding list. The following table lists the special characters that you can use in JavaScript strings. Character Meaning \b backspace \f form feed \n new line \r carriage return \t tab \ backslash character

4.5. Escaping characters

For characters not listed in the preceding table, a preceding backslash is ignored, with the exception of a quotation mark and the backslash character itself.

You can insert quotation marks inside strings by preceding them with a backslash. This is known as escaping the quotation marks. For example,

var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service." document.write(quote)

The result of this would be

He read "The Cremation of Sam McGee" by R.W. Service. To include a literal backslash inside a string, you must escape the backslash character. For example, to assign the file path c:\temp to a string, use the following:

var home = "c:\temp"

At this point you need to do at least one of two things:

  • read the code carefully, or better yet walk through the code with someone else, looking for the errant single or double quote

  • put console.log statements through your code to narrow down where the error is occuring.

The biggest thing to remember is that the parser is always right -- even when you think it's wrong, it's still right, because you can't negotiate with a parser.

Upvotes: 1

Josh K
Josh K

Reputation: 28883

It's very possible that if teststring contains a line break ("\n") that it will throw that kind of error because the JavaScript engine would see that as

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

I would recommend either ensuring that the teststring does not contain a line break or escaping it.

It's also possible that if teststring contains any single quotes it will terminate the string:

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

Upvotes: 2

Jaymz
Jaymz

Reputation: 6348

You should encode the html string, so <br/> becomes:

&lt;br/&gt;

Upvotes: -1

Related Questions