I have some Javascript code that needs to be dynamically created as a String and then "piped" onto the page via a JScript function.
e.g. Turn this:
<script type="text/javascript">
var so_id = new SWFObject("url", "so_id", 600, 400, "7", "#FFFFFF");
</script>
Into this:
var retString = '<script type="text/javascript">\n'
+ 'var so_' + id + ' = new SWFObject("'
+ url + '", "' + id + '", ' + width
+ ', ' + height + ', ' + player + ', "#FFFFFF");\n'
+ '</script>\n';
I am looking for a simple way to turn the first bit of code into a String as in the bottom example. Any quotes etc will need to be escaped.
In this particular case I do not need to escape anything, since I am using different types of quotes to surround the String than those inside. Ideally the solution would take care of these on it's own, but I am happy to specify which I am using.
At the moment I am escaping everything manually, but this is quite labour intensive.. I was just about to start on a JS / HTML form based parser, but wondered if anybody at SO has heard of / used anything like described?
EDIT
Bit of background:
We are using an online survey creation package called ConfirmIt, which uses JScript to add extra functionality (logic / ordering of questions etc). To get JavaScript onto the page, we compile a String using a JScript function like above, and then that is "piped" into the page on load. This means that I have a rather limited set of tools to use, so you will need to take this into consideration when answering.
EDIT 2
I wrote a JavaScript utility to do this with a switch to toggle between single and double quotes. As long as only one set of quotes is used consistently, then you don't have to worry about escaping 2 sets dynamically.