Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4248754
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:18:14+00:00 2026-05-21T04:18:14+00:00

I have some Javascript code that needs to be dynamically created as a String

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:18:15+00:00Added an answer on May 21, 2026 at 4:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a trivia page that is dynamically generated by some javascript code. It
I have some code that dynamically creates a new button through JavaScript that when
I have some Javascript code that needs to end with either a true or
I have some radio buttons that are created dynamically with javascript. I need to
I have page that has some javascript that needs to run at page load.
I have some code in a javascript file that needs to send queries back
I have some Javascript code that will programmatically register an COM interop assembly by
I have some JavaScript code that works in IE containing the following: myElement.innerText =
I have some javascript code that creates an img tag with a mouseover callback,
I have some Javascript code that creates 2 arrays: One for Product Category and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.