473,467 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

invalid text strings from db

I'm populating an array with string labels taken from a db.
Unfortunately the labels may contain characters such as the apostrophe
and the backslash so when I create the array:

myArray = [];
myArray.push('<%= str1 %>');
myArray.push('<%= str2 %>');
....

I can get javascript errors because the generated code contains lines
like this:

myArray.push('This is Andrew's page');

I have no control of the data in the db.
The strings get displayed in the page so they have to look "right".
I don't want to create a separate case for each "invalid" character.

What's the best way to handle this so that "invalid" characters are
handled correctly?

Andrew Poulos
Jan 21 '08 #1
4 1809
Andrew Poulos said the following on 1/20/2008 9:45 PM:
I'm populating an array with string labels taken from a db.
Unfortunately the labels may contain characters such as the apostrophe
and the backslash so when I create the array:

myArray = [];
myArray.push('<%= str1 %>');
myArray.push('<%= str2 %>');
...
Do you control the above code?
I can get javascript errors because the generated code contains lines
like this:

myArray.push('This is Andrew's page');
Yes.
I have no control of the data in the db.
Do you have control over the code that generates the page though?
The strings get displayed in the page so they have to look "right".
I don't want to create a separate case for each "invalid" character.

What's the best way to handle this so that "invalid" characters are
handled correctly?
That depends on whether you control the server side page that generates
the JS code or not. If you don't, then there is nothing you can do. If
you do, then you need to handle it on the server because by the time it
gets to the client, it is too late.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 21 '08 #2
Randy Webb wrote:
Andrew Poulos said the following on 1/20/2008 9:45 PM:
>I'm populating an array with string labels taken from a db.
Unfortunately the labels may contain characters such as the apostrophe
and the backslash so when I create the array:

myArray = [];
myArray.push('<%= str1 %>');
myArray.push('<%= str2 %>');
...

Do you control the above code?
Yes.
>I can get javascript errors because the generated code contains lines
like this:

myArray.push('This is Andrew's page');

Yes.
>I have no control of the data in the db.

Do you have control over the code that generates the page though?
Yes.
>The strings get displayed in the page so they have to look "right".
I don't want to create a separate case for each "invalid" character.

What's the best way to handle this so that "invalid" characters are
handled correctly?

That depends on whether you control the server side page that generates
the JS code or not. If you don't, then there is nothing you can do. If
you do, then you need to handle it on the server because by the time it
gets to the client, it is too late.
The server side code is written in ASP jscript that I can add to/modify.

Andrew Poulos

Jan 21 '08 #3
Andrew Poulos said the following on 1/20/2008 10:36 PM:
Randy Webb wrote:
>Andrew Poulos said the following on 1/20/2008 9:45 PM:
<snip>
>>The strings get displayed in the page so they have to look "right".
I don't want to create a separate case for each "invalid" character.

What's the best way to handle this so that "invalid" characters are
handled correctly?

That depends on whether you control the server side page that
generates the JS code or not. If you don't, then there is nothing you
can do. If you do, then you need to handle it on the server because by
the time it gets to the client, it is too late.

The server side code is written in ASP jscript that I can add to/modify.
Escape any backslashes and the enclosing quotes (whether single or
double). The only other one you might have to worry about would be line
feeds (CR/LF). Writing a simple routine to handle those two (maybe
three) isn't that much coding and not many "invalid" characters to deal
with.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 21 '08 #4
Randy Webb wrote on 21 jan 2008 in comp.lang.javascript:
><script type='text/javascript'>
var myArray = [];
myArray.push('<%= ChangeIt(str1) %>');
myArray.push('<%= ChangeIt(str2) %>');
</script>
========================================

I am curious why he uses push when creating the array. I know it keeps
you from having to keep up with the indexes but most of mine look like
this:

var myArrayLength = 0
myArray = new Array()
myArray[myArrayLength++] = "array entry here"
myArray[myArrayLength++] = "array entry here"

Then, I never have to look up the length, it is stored in
myArrayLength.

Any drawbacks to the approach I use?
I suppose here it is a matter of taste.
I never use push() in an array where I use placenumbers.

When using an array as lifo [stack] or fifo [linear] serial memory
push() and pop() are very usefull, together with shift() and unschift().
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 21 '08 #5

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

Similar topics

3
by: Kaidi | last post by:
Hello guys, I get the "an invalid XML character" error when using xerces to parse a XML file. I know that XML will correspond the &, <, >, " to special strings like "&gt;&lt;". However, how about if the...
30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
3
by: Sunny | last post by:
Hi, I'm using VS.net 2003 and C#. I created (using buildin designer) a dataset with 2 tables. TableOne has 2 fields: sName and sTable (both string) and a primary key TestKey, set on sName....
9
by: Mike MacSween | last post by:
So in preparing an export routine to send stuff to the courier company's label printing software (which is surprisingly rough, for a big company), I've discovered a few carriage returns in fields...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
12
by: Andy Sutorius | last post by:
Hi, I am getting the error "Invalid attempt to read when no data is present" when I run the following code. I have checked the sql and it is returning data. What am I missing? string...
3
by: Nathan Sokalski | last post by:
I am recieving the following error on the second postback of a page I have written: The state information is invalid for this page and might be corrupted Stack Trace: ...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
1
by: gowthamkg | last post by:
Hi, In this code i can get proper output only for numbers and for strings it gives error as invalid character.Please help me <% Private Function Decrypt(ByVal encryptedstring) Dim x, i,...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.