Fog Creek Software
Discussion Board




Knowledge Base
Documentation
Terry's Tips
Darren's Tips

Inserting x.body into Javascript

I'd like to use my x.body CityScript variable in some Javascript to do some processing on it. However, the body may contain both single quotes and double quotes - I can't really control that. Is there a way to bring the variable into Javascript without causing a Javascript error? Can you use some other sort of delimiter for Javascript strings besides ' and "?

David Watts
Saturday, November 13, 2004

Quotes in strings should be preceded by a backslash...
Alternatively, if your string includes single quotes only, then you can use double quotes as string delimiters, and vice versa.

http://www.javascripter.net/faq/quotesin.htm

I don't know if this kind of thing would be better to down on the server side or not.  I've heard PHP is Perl-like and Perl lets you quote() things: the quote function avoids these kind of problems).

David Burch
Monday, November 15, 2004

You could try this php solution...

Replace {$x.body$} with the following code:

<?php echo addslashes("{$x.body$}"); ?>

Then in CityDesk replace the ".html" template extension by ".php" so that the php code can be executed.

Finally don't forget to tell CityDesk that files with .php extensions may contain CityScript code (View -> File types...).

The php function addslashes protects the following characters by preceding a backslash:

single quote ( ' )
double quote  ( " )
backslash ( \ )
NUL (null byte)

Hope this works for you!

Regards

Stefan Ganz
http://www.citydeskwizards.com

Stefan Ganz
Tuesday, November 16, 2004

*  Recent Topics

*  Fog Creek Home