0

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.

3 Answers 3

1

This will turn that code into a string:

<script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script id="test" type="text/javascript">
var x = "asfasfasfsdsadf";
function xyz() {
    alert("Hello world");
}
</script>
<script type="text/javascript">
$(function() {
    alert( $("#test").text());
});
</script>

Though you may need to spaces instead of tabs. When I tried it in Chrome, it showed the tabs as special characters; Firefox had no problem.

Sign up to request clarification or add additional context in comments.

1 Comment

This was closest to what I did, so you win
1

Am I safe to assume you are doing some Ajax stuff with this and trying to dynamically run the javascript by injecting it into the page at somepoint?

If so then you might want to take a look at JSONP (JSON with Padding). Your server side script can return a function call which run on the browser when you grab it, basically a simply call back.

If I've got the wrong end of the stick then please provide a bit more context and I'll take another look at it :)

Cheers

Pete

Comments

1

This worked good for me

<script type="text/javascript">
function main(){
var a;
}

alert(main.valueOf());
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.