[Web-SIG] JavaScript escape function

Ian Bicking ianb at colorstudy.com
Tue Apr 26 03:18:33 CEST 2005


Shannon -jj Behrens 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?

FWIW, Python string literals seem to match Javascript's perfectly (at 
least the strings repr() produces -- not triple-quoted strings or 
anything fancy).  So you'd do "var s = $repr(str(s))".

-- 
Ian Bicking  /  ianb at colorstudy.com  / http://blog.ianbicking.org


More information about the Web-SIG mailing list