[Web-SIG] JavaScript escape function
Jp Calderone
exarkun at divmod.com
Tue Apr 26 03:19:27 CEST 2005
On Mon, 25 Apr 2005 18:11:46 -0700, Shannon -jj Behrens <jjinux at gmail.com> 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
More information about the Web-SIG
mailing list