http://qs321.pair.com?node_id=218598

perlcgi has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
First of all if you hate JavaScript, read no further dear Monk. Please don't downvote me, as I think this is an interesting chicken and egg problem that may interest many Monks, who some some day may encounter the same situation.
I am working with a HTML templating system, that is essentially, a black box. I cannot configure/hack it in any way. But I need to munge the expanded template codes for presentation, so I'm guessing the only tool I can use is JavaScript. (PerlScript here is not an option) For example the template code $123 might expand into "Hello World". Normally something like this works fine:
<script> var field = '$123'; document.write ('field'); </script>
But if the template code expands into "Hello Fred's World" then you'll get a syntax error as the apostrophe in Fred's is interpreted as the end of the string.
Is there a way to escape apostrophes and inverted comma characters before their assignment in JavaScript? Or is there some JavaScript trick to accommodate this situation?
I'd be obliged for any help Monk, and am thankful for your patience in allowing yourself to be exposed to this question.
perlcgi