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 anid
and usedocument.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
3 Answers
Reset to default 3var 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
版权声明:本文标题:string - Store Javascript code in javascript variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744766106a2624050.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论