473,466 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Use of \n in strings

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
5 3411
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

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

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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.