admin管理员组

文章数量:1392068

I'm trying to generate javascript code snippet and then put it to textarea, so that user could copy ready javascript code.

How to store something like this

"<script type='text/javascript'> 
  function init_map(){
  var myOptions = {
    zoom:14,center:new google.maps.LatLng(33.7593664,-118.14817399999998),
    mapTypeId: google.maps.MapTypeId.ROADMAP};
  }
</script>"

in javascript variable?

I'm trying to generate javascript code snippet and then put it to textarea, so that user could copy ready javascript code.

How to store something like this

"<script type='text/javascript'> 
  function init_map(){
  var myOptions = {
    zoom:14,center:new google.maps.LatLng(33.7593664,-118.14817399999998),
    mapTypeId: google.maps.MapTypeId.ROADMAP};
  }
</script>"

in javascript variable?

Share Improve this question asked Aug 3, 2014 at 12:56 giochgioch 1094 silver badges12 bronze badges 7
  • 1 So you're asking how to create a string that extends over multiple lines? Is that what you want to know? – cookie monster Commented Aug 3, 2014 at 12:57
  • New line is \n or \r\n, if you want line breaks in your literal, escape them with a single backslash too – Paul S. Commented Aug 3, 2014 at 12:58
  • Make the tag text/template, give it an id and use document.getElementById('id').textContent to get the string. jsfiddle/WfF57/1 – Jared Farrish Commented Aug 3, 2014 at 12:59
  • When i'm trying to store js code to varialbe, program runs it. I mean if i have <script src="somescript"></script>, i want just to show it in textarea, but javascript trying to include that file. – gioch Commented Aug 3, 2014 at 13:06
  • 1 @mplungjan not if it's an external js file? but I suppose we can't assume – Paul S. Commented Aug 3, 2014 at 13:08
 |  Show 2 more ments

3 Answers 3

Reset to default 3
var abc = "<script type='text/javascript'>\n" + 
  "function init_map(){\n"+
  "var myOptions = {\n"+
   "zoom:14,center:new google.maps.LatLng(33.7593664,-118.14817399999998),\n"+
    "mapTypeId: google.maps.MapTypeId.ROADMAP};\n"+
  "}\n"+
"<\/script>";
alert(abc);

here is the fiddle

You can do it as usual..its just an ASCII string so :

var temp = "<script type='text/javascript'>\n"+
"\tfunction init_map(){\n"+
"\tvar myOptions = {\n"+
"\t\tzoom:14,center:new google.maps.LatLng(33.7593664,-118.14817399999998),\n"+
"\t\tmapTypeId: google.maps.MapTypeId.ROADMAP};\n"+
"\t}\n"+
"<\/script>";

But I would suggest to use the "pre" html tag to display the code. Again everything is a matter of taste and maintainability.

I'm sure that there are way more elegant ways of dealing with code samples display.

You can always use an Open Source JavaScript code beautifier like which can output valid html etc. Have a look on this project : https://github./beautify-web/js-beautify

It powers the popular http://jsbeautifier/

You can use the \ character to continue the string on the following line, and also the \n character to create a new line, like this:

var string = "<script type='text/javascript'>\n\
  function init_map(){\n\
  var myOptions = {\n\
    zoom:14,center:new google.maps.LatLng(33.7593664,-118.14817399999998),\n\
    mapTypeId: google.maps.MapTypeId.ROADMAP};\n\
  }\n\
<\/script>" // the / in </script> needs also to be escaped

document.getElementById('your-text-area-id').value = string;

In your textarea you will see:

本文标签: stringStore Javascript code in javascript variableStack Overflow