我正在尝试获取javascript变量(foo)中的自定义通道字段(chan_body)的内容。我已经在config.php文件中设置了
$config['protect_javascript']="n"我有我的chan_body'Type'=>'TextArea'和‘Default Text Formatting=>'None’问题是这个通道字段实际上有几行长,实际上是另一种语言的代码(不打算执行),但它并没有被转义,只是被丢弃在那里,搞砸了javascript。我该怎么解决这个问题?我尝试了escape(),但对没有帮助
{exp:channel:entries channel="mychannel" category="2"}
<script type="text/javascript">
var foo = "{chan_body}";
alert(foo);
</script>
{/exp:channel:entries}
转换为
<script type="text/javascript">
var foo = "my $testing = "myfile.txt";
Uncaught SyntaxError: Unexpected identifier
open(FILE,"$myfile ") or die;
# this is a comment
alert(foo);
</script>
当分配给foo时,您可以使用base64编码来对chain_body中的值进行编码,并且在需要使用它的地方,您可以对其进行解码。
例如
var foo = BASE64_ENCODE("{chan_body}");
您可以在这里看到base64的内容是如何在javascript中工作的。如何用JavaScript将字符串编码为Base64?
JavaScript不太适合多行字符串。请参见如何创建多行字符串。要将其直接注入到写入的变量中,您需要在新行末尾使用反斜杠,并转义任何双引号。
但谁愿意这么做呢?
一种变通的方法可能是将字段的内容放在具有display:none
的div
中,并以这种方式访问它。
{exp:channel:entries channel="mychannel" category="2"}
<div id="entry-{entry_id}" style="display:none;">{chan_body}</div>
<script>
var foo = document.getElementById('entry-{entry_id}').innerHTML;
alert(foo);
</script>
{/exp:channel:entries}
$testing 后缺少报价
var foo = "my $testing" = "myfile.txt";