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

invalid text strings from db

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.