On Mon, 25 Apr 2005 18:11:46 -0700, Shannon -jj Behrens <[EMAIL PROTECTED]> wrote:
Hey guys,

I need a JavaScript escape function.  Let's say I have a variable, and
I'm generating some JavaScript from Cheetah:

   var s = "$s";

I need to make $s safe:

   var s = "$javascript_safe($s)";

Has anyone coded this yet?  Will the same function work for both
single and double quoted strings?  Can I steal some code?

Thanks :-D
-jj


Nevow has liveevil.jquote(), which I believe does what you want. It is strikingly simple and straightforward:

   def jquote(jscript):
       return jscript.replace('\\', '\\\\'
           ).replace("'", "\\'"
           ).replace('\n', '\\n')

 I wonder if it isn't buggy somehow :)  I don't know javascript well enough 
myself to say either way.

 Jp
_______________________________________________
Web-SIG mailing list
Web-SIG@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: 
http://mail.python.org/mailman/options/web-sig/archive%40mail-archive.com

Reply via email to