473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

newline in arrayelement

Hi,

From a PHP backend I generate a javascript array like this :

bevestigingen =
[{id:'47',leverancier:'VELUX',depot:'KNO',klant:'', werf:'',referentie:'Ro
els',producten:'DKL U04 1100 1st
DKL U04 1705 1st ...

The problem is that the 'producten' field contains data from a textarea
element, in the example 2 products, that were seperated by a newline. The
newline that was stored in the database, as it now emerges in html,
breaks the javascript code.

Is there a silver bullet for this, or should I look into a replacement
strategy serverside ?

thx

Ward
Jun 8 '06 #1
5 2934
Ward Germonpré wrote on 08 jun 2006 in comp.lang.javascript:
From a PHP backend I generate a javascript array like this :

bevestigingen =
[{id:'47',leverancier:'VELUX',depot:'KNO',klant:'', werf:'',referentie:'
Ro els',producten:'DKL U04 1100 1st
DKL U04 1705 1st ...

The problem is that the 'producten' field contains data from a
textarea element, in the example 2 products, that were seperated by a
newline. The newline that was stored in the database, as it now
emerges in html, breaks the javascript code.

Is there a silver bullet for this, or should I look into a replacement
strategy serverside ?


t = t.replace(/\n/g,' ')

Exchange the return for a space of whatever.
Better do this serverside if the string is from a db,
but PHP is beond the scope of this NG.

You should always validate any form input,
so that unwanted characters are replaced/deleted.

You could keep the return in a string litteral like this:

t = t.replace(/\n/g,'\\n')

Or have PHP do that.

Mee eens?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 8 '06 #2
Ward Germonpré wrote:
From a PHP backend I generate a javascript array like this :

bevestigingen =
[{id:'47',leverancier:'VELUX',depot:'KNO',klant:'', werf:'',referentie:'Ro
els',producten:'DKL U04 1100 1st
DKL U04 1705 1st ...

The problem is that the 'producten' field contains data from a textarea
element, in the example 2 products, that were seperated by a newline.
The newline that was stored in the database, as it now emerges in
html, breaks the javascript code.

Is there a silver bullet for this, or should I look into a replacement
strategy serverside ?


Generally, if you want to use arbitrary text data as the contents of a
javascript string literal you must _always_ transform various
characters into escape sequence equivalents of one sort or another.
These characters include: the backslash (which must be escaped first
when going in this direction as escape sequences commence with a
backslash), quote characters (either just the type being used to
delimit the string, or both types for ease/safety), all line
terminators (Line Feed, Carriage Return, and Unicode (hex) 2028 <LS>
and 2029 <PS>), backspace, form-feed, horizontal tabs and vertical tabs
(which should be escaped to Unicode or Hex escape sequences rather than
\v as IE/JScript has a bug with \v, which it considers a literal v).

There is no silver bullet, and this problem is the same as when
inserting arbitrary text into HTML when certain characters should be
substituted for entities.

Richard.

Jun 8 '06 #3
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in
news:11**********************@f6g2000cwb.googlegro ups.com:
Ward Germonpré wrote:
From a PHP backend I generate a javascript array like this :

bevestigingen
[{id:'47',leverancier:'VELUX',depot:'KNO',klant:'', werf:'',referentie:
'Ro els',producten:'DKL U04 1100 1st
DKL U04 1705 1st ...

The problem is that the 'producten' field contains data from a
textarea element, in the example 2 products, that were seperated by a
newline. The newline that was stored in the database, as it now
emerges in html, breaks the javascript code.

Is there a silver bullet for this, or should I look into a
replacement strategy serverside ?


Generally, if you want to use arbitrary text data as the contents of a
javascript string literal you must _always_ transform various
characters into escape sequence equivalents of one sort or another.
These characters include: the backslash (which must be escaped first
when going in this direction as escape sequences commence with a
backslash), quote characters (either just the type being used to
delimit the string, or both types for ease/safety), all line
terminators (Line Feed, Carriage Return, and Unicode (hex) 2028 <LS>
and 2029 <PS>), backspace, form-feed, horizontal tabs and vertical
tabs (which should be escaped to Unicode or Hex escape sequences
rather than \v as IE/JScript has a bug with \v, which it considers a
literal v).

There is no silver bullet, and this problem is the same as when
inserting arbitrary text into HTML when certain characters should be
substituted for entities.

Richard.


Thank you both for replying.

I used PHP's urlencode() en javascript's unescape() functions to get rid
of the above problem.

kind regards,

Ward
Jun 8 '06 #4
Ward wrote:
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in <snip>
Generally, if you want to use arbitrary text data as the contents of a
javascript string literal you must _always_ transform various
characters into escape sequence equivalents of one sort or another.

<snip> Thank you both for replying.
Apparently it was a waste of effort.
I used PHP's urlencode() en javascript's unescape() functions to
get rid of the above problem.


If you would rather use a ****-*** hack than do the job properly the
least you should do is use - decodeURIComponent - on the client (where
supported) so that the process is symmetrical.

Richard.

Jun 8 '06 #5
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in
news:11**********************@h76g2000cwa.googlegr oups.com:
Ward wrote:
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in

<snip>
Generally, if you want to use arbitrary text data as the contents of
a javascript string literal you must _always_ transform various
characters into escape sequence equivalents of one sort or another.

<snip>
Thank you both for replying.


Apparently it was a waste of effort.
I used PHP's urlencode() en javascript's unescape() functions to
get rid of the above problem.


If you would rather use a ****-*** hack than do the job properly the
least you should do is use - decodeURIComponent - on the client (where
supported) so that the process is symmetrical.

Richard.


Thanks for pointing me to decodeURIComponent.

I do take your advice to heart, in fact I'm rewriting an intranet app
but was asked to quickly implement a new functionality on the old '0.2
framewerkz'.., the new 1.0 app will have validation.

kind regards

Ward
Jun 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: S. Staats | last post by:
Good day, everbody. Here is a simple program called test.py: #!/usr/bin/python print "No newline, please.", # End of program Here is what program does: prompt> ./test.py No newline, please.
9
by: Alan Mackenzie | last post by:
To all those who use (X)Emacs's CC Mode to edit C, C++, Java, Objective-C, Pike, AWK or IDL: To help direct the development of CC Mode, it would be useful to find out how people use the...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
29
by: runningdog | last post by:
Hi, I would like to be able to embed a newline in a text string. Is there any convienent notation to do this TIA Steve
7
by: Alex Nordhus | last post by:
I am looking for a way to strip the blank line and the empty newline at the end of the text file. I can get the blank lines removed from the file but it always leaves the end line (which is blank)...
4
by: Peter Kirk | last post by:
Hi I would like to ask a little bit about the value Environment.Newline: what is it and what is the point of it? Ok, I can see in the docs that it represents "newline" for the current platform -...
5
by: Adam Right | last post by:
Hi, Is there a way to construct the mail body including newline characters by using .net framework mailing functions when sending an email? I cannot insert newline character into the body of the...
11
by: rossum | last post by:
I want to declare a const multi-line string inside a method, and I am having some problems using Environment.NewLine. I started out with: class foo { public void PrintStuff() { const...
1
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.