invalid text strings from db

A

Andrew Poulos

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
 
A

Andrew Poulos

Randy said:
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
 
E

Evertjan.

Andrew Poulos wrote on 21 jan 2008 in comp.lang.javascript:
Randy said:
Andrew Poulos said the following on 1/20/2008 9:45 PM:
myArray = [];

do not forget var.
[...]

The server side code is written in ASP jscript that I can add
to/modify.


Try:

========================================
<% //jscript assumed
function ChangeIt(x){
return x.replace(/'/g,"\\'");
};
%>

<script type='text/javascript'>
var myArray = [];
myArray.push('<%= ChangeIt(str1) %>');
myArray.push('<%= ChangeIt(str2) %>');
</script>
========================================
 
E

Evertjan.

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().
 

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,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top