简体   繁体   English

javascript中的表达式引擎字段

[英]expression engine fields in javascript

I am trying to get the content of a custom channel field (chan_body) inside a javascript variable (foo). 我想在javascript变量(foo)中获取自定义通道字段(chan_body)的内容。 I have set already in my config.php file 我已经在我的config.php文件中设置了
$config['protect_javascript'] = "n"; $ config ['protect_javascript'] =“n”; I have my chan_body 'Type' => 'TextArea' with 'Default Text Formatting => 'None' the problem is that this channel field is actually a couple lines long which is actually code in another language (that is not meant to be executed), but it's not getting escaped and just screwing up the javascript by getting dumped in there. 我有我的chan_body'Type '=>'TextArea''默认文本格式=>'无'问题是这个通道字段实际上是几行长,实际上是另一种语言的代码(不是要执行),但它没有逃脱,只是通过倾倒在那里搞砸javascript。 How can I fix this? 我怎样才能解决这个问题? I tried escape() that did not help 我尝试了逃避()没有帮助

{exp:channel:entries channel="mychannel" category="2"} 
    <script type="text/javascript">
        var foo = "{chan_body}";
        alert(foo);
    </script>    
{/exp:channel:entries}

translates to 翻译成

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

You can use base64 encode to encode value in chain_body when assigning to foo and where ever you need to use that, you can decode it. 在分配给foo时,你可以使用base64编码对chain_body中的值进行编码,你需要使用它来解码它。

For example 例如

var foo = BASE64_ENCODE("{chan_body}");

You can see here that how base64 stuff works in javascript. 你可以在这里看到base64的东西如何在javascript中工作。 How can you encode a string to Base64 in JavaScript? 如何在JavaScript中将字符串编码为Base64?

JavaScript doesn't take too well to multi-line strings. JavaScript对多行字符串不太好。 See How to create multi-line strings . 请参见如何创建多行字符串 To inject it directly to a variable as written you'd need backslashes \\ at the end of new lines, and also escape any double-quotation marks. 要将它直接注入到写入的变量中,您需要在新行的末尾处使用反斜杠\\ ,并且还要转义任何双引号。

But who wants to do that? 但是谁想这样做?

A round-about way might be to place the contents of your field in a div with display:none and access it that way. 圆形方式可能是将字段的内容放在带有display:nonediv ,并以这种方式访问​​它。

{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}

$测试后缺少报价

var foo = "my $testing" = "myfile.txt";

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

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