简体   繁体   English

包括 <script type=“text/javascript”>…</script> 在js变量中

[英]Include <script type=“text/javascript”>…</script> in js variable

I am trying to include the following script in a variable. 我试图在变量中包含以下脚本。

<script type="text/javascript">//<![CDATA[Calendar.setup({
inputField : "datum",trigger    : "f_btn1",onSelect   : function() {this.hide() },dateFormat : "%Y-%m-%d "});//]]></script>*

var contentString ='<table><tr><td><input size="10" id="datum" /><button id="f_btn1">ddd</td></tr> </table> <script type="text/javascript">..see above script...</script>';

The browser generates an error in the line with </script> . 浏览器在</script>所在的行中生成错误。 I cannot place the script because it should be executed only if a window opens in google maps --> infowindow.open(map,marker1); 我无法放置脚本,因为只有在google infowindow.open(map,marker1); > infowindow.open(map,marker1);打开一个窗口时才应执行该脚本infowindow.open(map,marker1);

Is it generally possible to put the declaration of js in a variable? 通常可以将js的声明放在变量中吗? I have tried several combinations of commas and escape variations (\\") but wasn't successful. 我尝试了逗号和转义变体(\\“)的几种组合,但未成功。

The browser will read the </script> tag inside the string, and assume that you're closing the JavaScript block entirely. 浏览器将读取字符串中的</script>标记,并假定您正在完全关闭JavaScript块。 So if you write: 因此,如果您写:

<script> var a = '</script>'; </script>

…it is not read as …它不被读作

<script>
  var a = '</script>';
</script>

But as 但是作为

<script>
  var a = '
</script>'; </script>

Usually people solve this by just splitting up the tag and concatenating it: 通常,人们可以通过拆分标签并将其串联来解决此问题:

<script> var a = '</scr' + 'ipt>'; </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM