How to handle this seneraio with String in JavaScript?

Hi there

I have store a user address in the database as

“Some Place,Some City
Some Value”

and when I try to get that value in JavaScript I see value like this

"var someVar=“Some Place,Some City
Some Value”
(By debugger in web browser)

however if I store some value like this

“var someVar='Some Place,Some City Some Value”

then it is working fine.problem is when a String is spilt in more than one line then it shows unterminated String leteral error in debug console in web browser

How to handle this seneraio?

Thanks

strip line breaks from the database values or fetch them through AJAX (where you do not print the values into a string expression) or replace the linebreaks by \n.

do you provide any code sample that How can I skip these line breaks?

echo str_replace(PHP_EOL, '\\n', $string);

Multi line strings are valid in all versions of JavaScript when you use \ to mark where the string ends on each line - like this:

var someVar="Some Place,Some City \
Some Value"

The latest version of JavaScript allows multiline strings wrapped in backticks but few browsers currently support that way.

But How can I know that when their is multiple line and when Single line String because String data is coming from database?

Multiple lines will contain a linefeed character somewhere in the string. You simply need to add a \ in front of each linefeed.

@felgall: at that stage you can as well replace the line feed characters.

depending on whether you want it split into multiple lines in the source so as to make it more readable

I don’t get any coding Idea where to place this “\n” How I suppose to know when text is spiliting in multiple line in HTML file?

you know that because of the line feed characters present in the text.

Know I understand problem of “\n”

But Question is suppose I have some data

“var someVar=‘Some Value,Some City
Some State’;
someVar.replace(”\n"," ");
myId.text(someVar);
"

but still getting error
How can I replace NewLine charcters

anyone on this issue?

This issue is still not solved

Did you try what Dormilich suggested in post 4?

I am using JSP what will be syntax for that
I can’t display that code with the Help of JSfiddle

because it is working fine on JSFiddle but not on web browser

I think I am close to newline and JavaScript behaviour its not directly related to jsp page

I am getting text in browser from db like this

“var multiLineString = ‘This is the 1st line
This is the 2nd line
This is the 3rd line…’;”

and valid syntax in javascript is
“var multiLineString = ‘This is the 1st line’ +
‘This is the 2nd line’ +
‘This is the 3rd line…’;”

so anyone can give me Idea how to add + at the start of a newline ?

the same way you wrapped each one in separate quotes so as to make them separate strings

No Idea till know how to convert a multiline string stored in that mysqldb to place and get that value in JavaScript variable
:sob:

Have you tried asking people who know JSP. That is after all the programming language you are using for this task. That the content of the strings you are generating contain JavaScript is not really relevant.

1 Like

Can you escape the string so that the newlines are shown as \n ?

The following also results in a multiline string:

var multiLineString = 'This is the 1st line\nThis is the 2nd line\nThis is the 3rd line...';

Result:

"This is the 1st line This is the 2nd line This is the 3rd line..."

Apache for example has an escapeJavaScript string method, for escaping strings for consumption by JavaScript.