Use of \n in strings

P

Patrick Nolan

I recently had need to use \n in a javascript string.
Firefox really doesn't like it. It says that I have an
unterminated string literal. I can't figure out how to
get around this.

I have a textarea element, and I want to append lines to it.
I tried
someArea.value += someString + "\n";
When I look at the page source, it looks like
someArea.value += someString + "
";

The string someString comes from a TEXT input element on a
form, so it doesn't come with its own \n.

Is there some way to accomplish this?
 
S

slebetman

I recently had need to use \n in a javascript string.
Firefox really doesn't like it. It says that I have an
unterminated string literal. I can't figure out how to
get around this.

I have a textarea element, and I want to append lines to it.
I tried
someArea.value += someString + "\n";
When I look at the page source, it looks like
someArea.value += someString + "
";

The string someString comes from a TEXT input element on a
form, so it doesn't come with its own \n.

Is there some way to accomplish this?

Works fine on MY Firefox 2 and 3. Are you SURE something on your
server end is not interpreting the \n before the browser sees it?
Here's a simple test case:



<html>
<head>
<script type="text/javascript">
window.onload = function () {
var someString = "This is a test";
var someArea = document.getElementById('foo');
someArea.value += someString + "\n";
}
</script>
</head>
<body>
<textarea id="foo">Hmm.. </textarea>
</body>
</html>
 
E

Erwin Moller

Patrick Nolan schreef:
I recently had need to use \n in a javascript string.
Firefox really doesn't like it. It says that I have an
unterminated string literal. I can't figure out how to
get around this.

I have a textarea element, and I want to append lines to it.
I tried
someArea.value += someString + "\n";
When I look at the page source, it looks like
someArea.value += someString + "
";

The string someString comes from a TEXT input element on a
form, so it doesn't come with its own \n.

Is there some way to accomplish this?


Hi,

It is not clear how your piece of code is implemented.
You say:
someArea.value += someString + "\n";

comes up as
someArea.value += someString + "
";
in the source??

The source was someArea.value += someString + "\\n";
So what is it excactly you are doing?

Anyway, give this a shot: Escape the backslash:
someArea.value += someString + "\\n";


Regards,
Erwin Moller

--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
 
E

Erwin Moller

Erwin Moller schreef:
Patrick Nolan schreef:


Hi,

It is not clear how your piece of code is implemented.
You say:
someArea.value += someString + "\n";

comes up as
someArea.value += someString + "
";
in the source??

The source was someArea.value += someString + "\\n";

Typo:
I ment of course:
The source was someArea.value += someString + "\n";
So what is it excactly you are doing?

Anyway, give this a shot: Escape the backslash:
someArea.value += someString + "\\n";


Regards,
Erwin Moller


--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
 
P

Patrick Nolan

Patrick Nolan schreef:

Hi,

It is not clear how your piece of code is implemented.
You say:
someArea.value += someString + "\n";

comes up as
someArea.value += someString + "
";
in the source??
Thanks for the response. I am a bonehead.
The web page is produced by a perl CGI script. The javascript
part of it is in a long block of literal text surrounded by
print <<EOF; and EOF. I forgot that perl would interpret the
\n embedded in that. Maybe I never knew it. Replacing \n
with \\n cures the problem.

Maybe I should start putting the javascript into a separate
file, rather than combining it with the generated HTML.
 
E

Erwin Moller

Patrick Nolan schreef:
Thanks for the response. I am a bonehead.
The web page is produced by a perl CGI script. The javascript
part of it is in a long block of literal text surrounded by
print <<EOF; and EOF. I forgot that perl would interpret the
\n embedded in that. Maybe I never knew it. Replacing \n
with \\n cures the problem.

Maybe I should start putting the javascript into a separate
file, rather than combining it with the generated HTML.

Hi,

If you DON'T need any variable substitution IN the HERE DOC, you could
use single quotes instead of ".
That might avoid the \n problem.
But I am not 100% sure, since I dropped PERL the same day I saw PHP,
long ago. ;-)

Regards,
Erwin Moller


--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top