By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 1,985 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

Use of \n in strings

P: n/a
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?
Sep 24 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Sep 25, 7:20 am, Patrick Nolan <p...@glast2.Stanford.EDUwrote:
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>

Sep 25 '08 #2

P: n/a

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/
============================
Sep 25 '08 #3

P: n/a

Erwin Moller schreef:
>
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";
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/
============================
Sep 25 '08 #4

P: n/a
On 2008-09-25, Erwin Moller wrote:
>
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 + "
";
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.
Sep 25 '08 #5

P: n/a

Patrick Nolan schreef:
On 2008-09-25, Erwin Moller wrote:
>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 + "
";
>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.
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/
============================
Sep 26 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.